我有一个UICollectionViewCell
,其中包含一个UIView
,其中包含一个UILabel
。我必须在布尔值的基础上更改UIView
的背景颜色和UILabel
的文本颜色。布尔值基础上的颜色工作,除非我在UICollectionView
中添加第四项。添加第四项后,所有以前和当前单元格中的所有项目都会获得与第四个单元格的项目相同的颜色。
问题与但这但在这里我无法在UICollectionViewCell
的init
方法中添加颜色,然后根据布尔值在cellForRowAt
中激活/停用它们。我该怎么做才能解决这个问题?
自定义UICollectionViewCell
的方法中重置UIView
和UILabel
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
}
}