是否有任何方法可以将 UICollectionViewCell 设置为“不可选择”,或者是"didSelectItemAtIndexPath"通过查询正确的单元格类型的委托方法来执行此操作的唯一方法?
提前致谢。
想象一下这样的场景,UICollectionView在每个单元格中,您希望有一个按钮填充整个单元格,以便您可以响应各种触摸事件来处理高光外观。例如,当用户触摸按钮时,您想更改按钮的背景颜色,然后在拖出或取消触摸等时将其还原。现在想象一下您想要更改按钮的背景颜色而不是更改按钮的背景颜色的情况细胞的backgroundView. AUIButton没有背景视图,只有 abackgroundColor或backgroundImage。
我有一个解决方案,但我想知道它是否可以更清洁,如果不推荐这种方法。触摸按钮后,我会遍历它的superviews 直到得到 ,UICollectionViewCell然后将其selected属性设置为true。在cellForItemAtIndexPath我selectedBackgroundView根据需要设置。这样就得到了想要的行为,但是用selected状态来表示高亮状态,这样管理是不是不合适?什么会更好?
我可以UICollectionViewCell在触摸按钮后更改其backgroundView属性,而不是在创建每个单元格时执行此操作,然后就无需更改selected值。但这仍然不是一个很好的解决方案。
我正在下载一些图像并将它们插入到我的 UICollectionView 中
这些是我的班级成员
var myItems:[UIImage] = []
var myView: UICollectionView?
Run Code Online (Sandbox Code Playgroud)
我有一个接收 img:UIImage 对象的计时器函数。我将其插入到我的 UICollectionView 中,如下所示
self.myItems.insert(img, atIndex: self.myItems.count)
var count:Int? = self.myView?.numberOfItemsInSection(0)
var index:NSIndexPath = NSIndexPath(forRow: count!, inSection: 0)
self.myView?.insertItemsAtIndexPaths([index])
Run Code Online (Sandbox Code Playgroud)
我也有这个功能:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.myItems.count;
}
Run Code Online (Sandbox Code Playgroud)
和这个:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as MyCollectionViewCell
cell.backgroundColor = UIColor.orangeColor()
cell.textLabel.text = "Text \(indexPath.row)"
cell.imageView.image = myItems[indexPath.row]
cell.backgroundView = UIImageView(image: UIImage(named: "cellbg"))
return cell
} …Run Code Online (Sandbox Code Playgroud) 我希望一个视图控制器有七个水平按钮滚动。我已经设置了第一个集合视图,并在正下方重复了第二个集合视图的确切过程,但我不断收到线程 1:SIGABRT 错误。我的功能集合视图的代码在这里:
class xyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var tableImages: [String] = ["1.png", "2.png", "3.png", "4.png", "5.png", "6.png"]
override func viewDidLoad() {
// Initialize the collection views, set the desired frames
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tableImages.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:xyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! xyCollectionViewCell
cell.xyImgCell.image = UIImage(named: tableImages[indexPath.row])
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
println("cell \(indexPath.row) selected")
} …Run Code Online (Sandbox Code Playgroud) 编辑:似乎与此相同的问题
我使用自定义布局和单元格制作了集合视图,但我的单元格的位置有点偏离,所以我决定给它们编号,以便我可以更轻松地调试它。我按照这个人的回答使用标签为我的单元格编号。不知何故myLabel为零并导致我的应用程序崩溃。
我相信我已经正确连接了插座,但也许有人可以提供一个列表让我检查我是否做错了什么。
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet var gridView: UICollectionView!
private let reuseIdentifier = "DesignCell"
private let numberOfSections = 1
private let numberOfCircles = 48
override func viewDidLoad() {
super.viewDidLoad()
self.gridView.registerClass(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return numberOfSections
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfCircles
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = …Run Code Online (Sandbox Code Playgroud) ios uicollectionview uicollectionviewcell uicollectionviewlayout swift
import UIKit
class ActionCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var actionGIF: UIImageView!
@IBAction func actionPressed(sender: AnyObject) {
print(myLabel.text)
Global.actionButtonIndex = myLabel.text!.toInt()! - 1
print(actionGIF.image)
ActionViewController.performSegueWithIdentifier("showActionPreview", sender: nil)
}
Run Code Online (Sandbox Code Playgroud)
}
我试图在用户单击集合视图中的一个单元格后执行 Segue。使用performSegueWithIdentifier 似乎无法做到这一点。应用程序截图
嘿我正在尝试使用集合视图单元在视图控制器中显示一组"标签",但是我很难找到一种方法使它们能够根据字符串的长度动态调整大小.
现在,单个单元格是静态大小的,因此只要字符串填充单元格超过单元格大小的字符串,它就会进入第二行.我想要它,以便单元格可以根据字符串的长度更改长度.因此,如果它是标记"#Vegan",它将自动调整大小以使标记不那么大.同样,如果它是一个更长的字符串,如"#LaptopFriendly",它将水平变长以容纳字符串而不使用第二行.垂直长度可以保持固定.谢谢!
UPDATE(使用Rob的代码遇到错误时的界面构建器设置):
这个问题(及其答案)让我达到了我想要的一半。
但是现在我被困在下一部分并且(说实话)我什至不确定这是否可能。
我已经定义了一个像这样的单元格......
class ExpandableCell: UICollectionViewCell {
var priority: Expandable.Priority = .low {
didSet {
imageView.isHidden = priority != .high
}
}
private let imageView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .red
v.heightAnchor.constraint(equalTo: v.widthAnchor).isActive = true
return v
}()
private let label: UILabel = {
let l = UILabel()
l.translatesAutoresizingMaskIntoConstraints = false
l.font = UIFont.preferredFont(forTextStyle: .headline)
l.numberOfLines = 0
l.text = "Tap Me!"
l.setContentCompressionResistancePriority(.required, for: .vertical)
l.adjustsFontForContentSizeCategory = true
l.textAlignment = .center
return …Run Code Online (Sandbox Code Playgroud) ios uicollectionview uicollectionviewcell uicollectionviewlayout swift
我已经设置了一个 UICollectionView ,如下所示。我正在尝试设置自定义单元格高度,以便集合在必须滚动之前适合屏幕上的 3 个单元格。但是,这种定制工作不起作用或似乎被忽略了。有谁知道我做错了什么?
class CoopOverviewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var CampaignBrandSliderBackground: UIView!
@IBOutlet weak var CampaignBrandSlider: UISegmentedControl!
@IBOutlet weak var CampaignCollectionView: UICollectionView!
// TODO: load real data and replace with campaign model
let campaignImages: [UIImage] = [UIImage(named: "icon-black")!,UIImage(named: "icon-white")!,UIImage(named: "icon-black")!,UIImage(named: "icon-white")!]
let apiService = APIService()
override func viewDidLoad() {
super.viewDidLoad()
CampaignBrandSliderBackground.layer.cornerRadius = 10
// do other stuff...
// Conform to UICollectionViewDelegate Protocol:
CampaignCollectionView.dataSource = self
CampaignCollectionView.delegate = self
// Adjust Layout of CollectionViewCell: set cell height …Run Code Online (Sandbox Code Playgroud) 将手机更新到 iOS 14 后,我无法再与应用内UICollectionViewCell或UITableViewCell应用内的内容进行交互。该问题也存在于运行 iOS 14 的 iOS 模拟器中,但在 iOS 13 及更低版本上一切正常。
我在不再可交互的单元格中有按钮和其他集合视图。就好像整个单元格内容都变成了静态。下面是来自我UICollectionViewCells和的一些代码UICollectionViewController,但是同样的问题存在于UITableViewCells. 我正在使用 Swift 5.didSelectItemAt并且类似的功能工作正常,它在单元格内进行交互,但contentView似乎无法正常工作。如有必要,我可以粘贴更多代码,因为我修剪了一些绒毛和其他我缩小范围的代码,这不是问题。
由于这个问题存在于两者中UICollectionViewCell,UITableViewCell我相信我在更基本的层面上做错了。
相关代码来自UICollectionViewCell:
override init(frame: CGRect) {
super.init(frame: frame)
setupContainers()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupContainers() {
// Adding subviews like so...
addSubview(subviewToAdd)
// Anchor all subviews using NSConstraints
}
Run Code Online (Sandbox Code Playgroud)
然后从相关代码 UICollectionViewController
override func viewDidLoad() {
super.viewDidLoad() …Run Code Online (Sandbox Code Playgroud)