[英]Strange bug on dequeuing collectionviewcell
I have a calendarView
made up of collectionView
. 我有一个由
collectionView
组成的calendarView
。 It is a custom calendarView
derived using mathematical calculations. 它是使用数学计算得出的自定义
calendarView
。 The seventh row marks Saturday and it's holiday so the font color is red for the all the labels of seventh column.
第七行标记星期六和假日,因此第七列的所有标签的字体颜色为红色。 However, when I swipe or navigate to other days, the red color labels are scattered in random order which is untraceable.
但是,当我滑动或导航到其他日子时,红色标签以随机顺序分散,这是无法追踪的。 A screenshot is herewith:
屏幕截图如下:
How did this occur? 这是怎么发生的? In my
dequeueReusableCell
method I have cell configured for holiday as: 在我的
dequeueReusableCell
方法中,我将单元格配置为假日:
cell.isHoliday = (indexPath.row + 1) % 7 == 0 ? true : false
And this is the logic for holiday in my custom collectionViewCell
. 这是我的自定义
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
}
}
}
The number of red labels on top of each collectionview cells goes on increasing as I swipe to next month. 随着我滑动到下个月,每个集合视图单元格顶部的红色标签数量不断增加。 Why is this happening and how can I stop this from happening?
为什么会发生这种情况?如何阻止这种情况发生?
You are missing else part: 你缺少其他部分:
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
}
}
}
This may be because the cell is being reused and you are not implemented any logic in prepareForReuse method of your custom cell class. 这可能是因为单元正在重用,并且您没有在自定义单元类的prepareForReuse方法中实现任何逻辑。 In this method try setting text colour properties to nil.
在此方法中,尝试将文本颜色属性设置为nil。
The right way to deal with old data showing up in reused cells is to override prepeareForReuse
in your custom cell 处理重用单元格中显示的旧数据的正确方法是覆盖自定义单元格中的
prepeareForReuse
open override func prepareForReuse() {
super.prepareForReuse()
tithiLabel.textColor = UIColor.black
dateLabel.textColor = UIColor.black
englishDateLabel.textColor = UIColor.black
}
Clear out old values (by assigning them to nil
) or set defaults to all values that might not necessarily be set after the cell is reused. 清除旧值(通过将它们指定为
nil
)或将默认值设置为在重复使用单元格后可能不一定设置的所有值。 This way, even if the new value(s) are not explicitly set to the cell, you are sure that old values are not being retained. 这样,即使未将新值明确设置为单元格,也可以确保不保留旧值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.