【发布时间】:2015-12-26 06:58:10
【问题描述】:
我使用 File > New > File... > Cocoa Touch Class 的 Xcode UI 创建了我的子类(同时检查了创建 xib 文件)... 完成。
我将子视图直接添加到集合视图单元格的view 子视图中:
我已经用UICollectionView 注册了子类和笔尖,一切都很好。但是,当我以编程方式访问我的集合视图单元格的 contentView 属性来设置其背景颜色时,它并没有改变。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CellIdentifiers.Category, forIndexPath: indexPath) as! CategoryCollectionViewCell
let category = categoryForIndexPath(indexPath)
cell.textLabel.text = titleForCategory(category)
cell.contentView.backgroundColor = UIColor.redColor()
return cell
}
在 lldb 中,我的 contentView 的地址是:0x13fdc3550,而文本标签的超级视图(我认为应该是 contentView)的地址是 0x000000013fdc36b0 -所以它们一定是不同的。
如何使用自定义 xib 成功继承 UICollectionViewCell 并通过 contentView 属性访问内容视图?
P.s,有趣的是,如果我将这个 contentView 属性的 hidden 设置为 true,就可以了。
【问题讨论】:
-
尝试在
collectionView(_:willDisplayCell:forItemAtIndexPath:)中设置背景色 -
很遗憾这不起作用
标签: ios xib uicollectionviewcell