ios - 动态处理 UICollectionViewCell 元素内的背景和前景色
问题描述
我有一个UICollectionViewCell
包含一个UIView
封闭的 a UILabel
。我必须在布尔值的基础上更改背景颜色UIView
和文本颜色。UILabel
除非我在UICollectionView
. 一旦我添加了第四个项目,来自所有先前和当前单元格的所有项目都会获得与第四个单元格的项目相同的相应颜色。
问题与此完全相关,
但在这里我不能在init
方法中添加颜色,UICollectionViewCell
然后cellForRowAt
根据布尔值激活/停用它们。我能做些什么来解决这个问题?
解决方案
重置自定义的colors
ofUIView
和UILabel
inprepareForReuse()
方法UICollectionViewCell
。
例子:
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
}
}
推荐阅读
- python - 有没有办法专门优化张量流中神经网络的单个输出?
- regex - 使用正则表达式 hive sql 从表中提取行
- ruby-on-rails - 在 Rails 5 中检查对象是否为 ApplicationRecord
- r - 重复顶点 ID R
- python - 将 pandas 数据帧转换为有向 networkx 多重图
- flutter - 无法在颤振中同步 build.gradle 中的依赖项
- android - 将变量传入
的布局标签 - database - 查找每条记录的交叉表的最新条目?
- ember.js - 在 Ember 中使用多个 yield 在不同位置显示页眉、正文、页脚
- database - Laravel 5.7 迁移 - 如何在可为空的外键上删除级联