出队 collectionviewcell 的奇怪错误
Strange bug on dequeuing collectionviewcell
我有一个 calendarView
由 collectionView
组成。它是使用数学计算得出的自定义 calendarView
。
第七行标记星期六和假期,因此第七列所有标签的字体颜色为红色。
但是,当我滑动或导航到其他日期时,红色标签以无法追踪的随机顺序散落。附上截图:
这是怎么发生的?
在我的 dequeueReusableCell
方法中,我将单元格配置为假期:
cell.isHoliday = (indexPath.row + 1) % 7 == 0 ? true : false
这就是我的习惯中假期的逻辑 collectionViewCell
。
@IBOutlet var dateLabel: UILabel!
@IBOutlet var englishDateLabel: UILabel!
@IBOutlet var tithiLabel: UILabel!
var isToday: Bool = false {
didSet {
self.contentView.backgroundColor = isToday ? Colors.Palette.LightGreen : UIColor.white
}
}
var isHoliday: Bool = false {
didSet {
if isHoliday {
tithiLabel.textColor = Colors.Palette.DarkRed
dateLabel.textColor = Colors.Palette.DarkRed
englishDateLabel.textColor = Colors.Palette.DarkRed
}
else {
dateLabel.textColor = UIColor.black
englishDateLabel.textColor = UIColor.black
}
}
}
当我滑动到下个月时,每个 collectionview 单元格顶部的红色标签数量不断增加。为什么会发生这种情况,我该如何阻止这种情况发生?
您缺少其他部分:
var isHoliday: Bool = false {
didSet {
if isHoliday {
tithiLabel.textColor = Colors.Palette.DarkRed
dateLabel.textColor = Colors.Palette.DarkRed
englishDateLabel.textColor = Colors.Palette.DarkRed
}
else {
tithiLabel.textColor = UIColor.black
dateLabel.textColor = UIColor.black
englishDateLabel.textColor = UIColor.black
}
}
}
这可能是因为正在重复使用单元格,而您没有在自定义单元格的 prepareForReuse 方法中实现任何逻辑 class。在此方法中,尝试将文本颜色属性设置为 nil。
处理重复使用的单元格中出现的旧数据的正确方法是覆盖自定义单元格中的 prepeareForReuse
open override func prepareForReuse() {
super.prepareForReuse()
tithiLabel.textColor = UIColor.black
dateLabel.textColor = UIColor.black
englishDateLabel.textColor = UIColor.black
}
清除旧值(通过将它们分配给 nil
)或将默认值设置为所有在重复使用单元格后可能不必设置的值。这样,即使新值未明确设置到单元格,您也可以确保不会保留旧值。
我有一个 calendarView
由 collectionView
组成。它是使用数学计算得出的自定义 calendarView
。
这是怎么发生的?
在我的 dequeueReusableCell
方法中,我将单元格配置为假期:
cell.isHoliday = (indexPath.row + 1) % 7 == 0 ? true : false
这就是我的习惯中假期的逻辑 collectionViewCell
。
@IBOutlet var dateLabel: UILabel!
@IBOutlet var englishDateLabel: UILabel!
@IBOutlet var tithiLabel: UILabel!
var isToday: Bool = false {
didSet {
self.contentView.backgroundColor = isToday ? Colors.Palette.LightGreen : UIColor.white
}
}
var isHoliday: Bool = false {
didSet {
if isHoliday {
tithiLabel.textColor = Colors.Palette.DarkRed
dateLabel.textColor = Colors.Palette.DarkRed
englishDateLabel.textColor = Colors.Palette.DarkRed
}
else {
dateLabel.textColor = UIColor.black
englishDateLabel.textColor = UIColor.black
}
}
}
当我滑动到下个月时,每个 collectionview 单元格顶部的红色标签数量不断增加。为什么会发生这种情况,我该如何阻止这种情况发生?
您缺少其他部分:
var isHoliday: Bool = false {
didSet {
if isHoliday {
tithiLabel.textColor = Colors.Palette.DarkRed
dateLabel.textColor = Colors.Palette.DarkRed
englishDateLabel.textColor = Colors.Palette.DarkRed
}
else {
tithiLabel.textColor = UIColor.black
dateLabel.textColor = UIColor.black
englishDateLabel.textColor = UIColor.black
}
}
}
这可能是因为正在重复使用单元格,而您没有在自定义单元格的 prepareForReuse 方法中实现任何逻辑 class。在此方法中,尝试将文本颜色属性设置为 nil。
处理重复使用的单元格中出现的旧数据的正确方法是覆盖自定义单元格中的 prepeareForReuse
open override func prepareForReuse() {
super.prepareForReuse()
tithiLabel.textColor = UIColor.black
dateLabel.textColor = UIColor.black
englishDateLabel.textColor = UIColor.black
}
清除旧值(通过将它们分配给 nil
)或将默认值设置为所有在重复使用单元格后可能不必设置的值。这样,即使新值未明确设置到单元格,您也可以确保不会保留旧值。