【发布时间】:2016-03-27 18:06:27
【问题描述】:
在下面的代码中,目标是为每个 UICollectionViewCell 动态添加一个 UIView。但是,UIView 只会添加到第一个单元格。
在调用doInit 时,打印 UICollectionViewCell 框架显示该框架是正确的,那么为什么代码只适用于第一个单元格?
UICollectionView 代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// Get cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PantryStyleCellIdentifer, forIndexPath: indexPath) as! PantryStyleCell
// Get style & init cell
let style = pantry[indexPath.row]
cell.doInit(style)
// Return cell
return cell
}
UICollectionViewCell 代码:
class PantryStyleCell: UICollectionViewCell {
var _style : BlockStyle?
func doInit(style: BlockStyle) {
// Store vars
_style = style
let testView = UIView(frame: frame)
testView.backgroundColor = gGreenColor
addSubview(testView)
}
【问题讨论】:
-
也许你应该在 self.contentView 中查看它。不知道怎么了。其实你不应该那样做,因为当再次重用视图时,会有2个testView,
标签: ios swift uiview uicollectionview uikit