因此,我需要扩展UICollectionView功能,覆盖单元格等。在每个示例中,我都看到了UICollectionViewController的子类化,但不仅仅是UICollectionView。
我不想让我的新类(名为GridSet)作为故事板中单个页面(UIViewController)的超类,因为我有更多的元素(自定义按钮、标签等),我不想在新类中维护它们,但我想在我的页面UIViewController中维护它们。
我甚至想在一个UIViewController中插入两个子类,并在故事板中这样做(但您不能将一个UIViewController放在另一个UIViewController中。
问题是,当我对UICollectionView和UICollectionViewDataSource进行子类化时,它似乎甚至都没有初始化。
class GridSet: UICollectionView, UICollectionViewDataSource {
let computers = [
["Name" : "MacBook Air", "Color" : "Silver"],
["Name" : "MacBook Pro", "Color" : "Silver"],
["Name" : "iMac", "Color" : "Silver"],
["Name" : "Mac Mini", "Color" : "Silver"],
["Name" : "Mac Pro", "Color" : "Black"]
]
override func awakeFromNib() {
super.awakeFromNib()
let nib = UINib(nibName: "GridSetCell", bundle: nil)
self.registerNib(nib, forCellWithReuseIdentifier: "GridSetCellDefault")
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return computers.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = self.dequeueReusableCellWithReuseIdentifier("GridSetCellDefault", forIndexPath: indexPath) as GridSetCell
cell.label = computers[indexPath.row]["Name"]!
return cell
}
}我将nib用于自定义单元格:
class GridSetCell: UICollectionViewCell {
@IBOutlet private weak var cLabel: UILabel!
@IBOutlet private weak var cBack: UIImageView!
var label: String = "" {
didSet {
if (label != oldValue) {
cLabel.text = label
}
}
}
var back: String = "" {
didSet {
if (back != oldValue) {
cBack.image = UIImage(named: back)
}
}
}
}发布于 2015-02-11 09:55:14
首先,使用系统UICollectionView中的版本对UICollectionViewCell和UICollectionViewLayout进行子类化,以实现您的目标。本教程展示了这些步骤:http://skeuo.com/uicollectionview-custom-layout-tutorial如果您仍然需要创建UICollectionView的子类,那么您将拥有可以“安装”到新的子类中的工作组件。
https://stackoverflow.com/questions/27445287
复制相似问题