我有一个包含包含动态更新的UILabel的单元格的UICollectionView。选择单元格时,单元格的背景颜色会发生变化,但是我希望标签中的文本颜色也发生变化。当前,我正在使用以下代码:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
cell = collectionView.dequeueReusableCellWithReuseIdentifier("targetCell", forIndexPath: indexPath) as UICollectionViewCell
var label : UILabel = cell.viewWithTag(100) as UILabel
label.textColor = UIColor.whiteColor()
}
但是,在选择单元格时,文本颜色不会更新为新颜色。有什么想法吗?
因为您要使可重用单元出队并对其进行更新。
基本上,dequeueReusableCellWithReuseIdentifier:forIndexPath:
仅用于中collectionView:cellForItemAtIndexPath:
。
使用cellForItemAtIndexPath
获取所选单元格。
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) {
let label = cell.viewWithTag(100) as? UILabel
label?.textColor = UIColor.white
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句