简体   繁体   中英

Strange bug on dequeuing collectionviewcell

I have a calendarView made up of collectionView . It is a custom calendarView derived using mathematical calculations. 这是第七列中的标签为红色的理想情况 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:

cell.isHoliday = (indexPath.row + 1) % 7 == 0 ? true : false

And this is the logic for holiday in my custom 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. In this method try setting text colour properties to nil.

The right way to deal with old data showing up in reused cells is to override prepeareForReuse in your custom cell

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM