【发布时间】:2014-12-12 14:00:48
【问题描述】:
所以,我需要扩展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
}
}
我将笔尖用于自定义单元格:
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)
}
}
}
}
【问题讨论】:
标签: ios swift uicollectionview