滚动屏幕并重新打开时,UICollectionView会覆盖单元格标签

时间:2015-07-08 17:35:58

标签: ios uicollectionview

我有一个带有UICollectionView的测试应用程序,当它们第一次出现在屏幕上时会正确地绘制它的单元格,但是用新的文本覆盖标签文本,当单元格在屏幕上滚动并返回时,它会以不同的方式放置。< / p>

以下是应用打开时的外观: Original view

以下是滚动关闭后的外观:

after scrolling

这是使单元格出列并添加标签的代码:

func collectionViewTableLayoutManager(manager: DRCollectionViewTableLayoutManager!, collectionView: UICollectionView!, cellForRow row: UInt, column: UInt, indexPath: NSIndexPath!) -> UICollectionViewCell! {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewCellIdentifier, forIndexPath: indexPath) as! UICollectionViewCell

cell.layer.borderColor = UIColor.blueColor().CGColor
cell.layer.borderWidth = 1

let subViews = cell.contentView.subviews

let labels = subViews.filter {$0 is UILabel } as! [UILabel]
let label: UILabel


if let lbl = labels.first {
    label = lbl

} else {

    label = UILabel(frame: cell.bounds)
    label.font = UIFont.systemFontOfSize(10)
    label.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
    label.textAlignment = NSTextAlignment.Center
    label.backgroundColor = UIColor.clearColor()
    cell.addSubview(label)
}

label.text = "\(indexPath.section):\(indexPath.row) / \(row):\(column)"

return cell

}

有什么想法吗?它是label.text行,并且单元格为IndexPath和行和列获取不同的值。我虽然保持同步,但正确地用正确的信息绘制单元格。 (这在Obj C中都可以正常工作)

1 个答案:

答案 0 :(得分:2)

您正在直接向单元格添加标签,但检查单元格contentView中是否存在标签。尝试将标签添加到单元格的contentView,而它应该可以正常工作。

通常,如果您知道所有这些单元格都需要该标签,则应创建一个新单元格类,其中已包含自己的标签。这样,集合视图数据源不必检查单元格内容并有条件地创建标签;标签已经存在,等待接受字符串。

相关问题