动态处理 UICollectionViewCell 元素内的背景色和前景色



我有一个UICollectionViewCell,其中包含一个UIView,其中包含一个UILabel。我必须在布尔值的基础上更改UIView的背景颜色和UILabel的文本颜色。布尔值基础上的颜色工作,除非我在UICollectionView中添加第四项。添加第四项后,所有以前和当前单元格中的所有项目都会获得与第四个单元格的项目相同的颜色。

问题与但这但在这里我无法在UICollectionViewCellinit方法中添加颜色,然后根据布尔值在cellForRowAt中激活/停用它们。我该怎么做才能解决这个问题?

自定义UICollectionViewCell的方法中重置UIViewUILabel prepareForReuse() colors

例:

class CustomCell: UICollectionViewCell {
    @IBOutlet weak var customView: UIView!
    @IBOutlet weak var label: UILabel!
    override func prepareForReuse() {
        super.prepareForReuse()
        //reset the colors here.........
        customView.backgroundColor = .white
        label.textColor = .black
    }
}

最新更新