ios - 使collectionviewcell出队的奇怪错误

标签 ios swift3 uicollectionview uicollectionviewcell

我有一个由calendarView组成的collectionView。它是使用数学计算得出的自定义calendarView
This is ideal situation where labels in seventh column is red
第七行标记为星期六,是假日,因此第七列的所有标签的字体颜色均为红色。
但是,当我滑动或导航到其他日子时,红色标签以随机顺序散落,这是无法追踪的。截图如下:And voila, here come those random colors on two right swipes

这是怎么发生的?
在我的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
        }
    }
}

关于ios - 使collectionviewcell出队的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43999466/

相关文章:

swift - UIScrollView 中的 UITableview 和 UICollectionView - Swift

ios - 尝试将 React Native 项目构建到设备时出现奇怪的错误和红屏

arrays - 为什么数组不符合 Equatable,而它的项在 Swift 中是 Equatable 的?

ios - 将 LocalNotification 显示为警报

Swift 3 转换类型别名闭包转换问题

ios - 使用Swift 3将捕获的图像存储在iOS中

ios - 如何创建 Collection View 并以编程方式将其添加到 ViewController?

ios - UICollectionView 具有自动布局的自定尺寸单元格

ios - “order”(bootstrap 4 网格)在 Safari IOS 中无法正常工作

ios - 字体在 Storyboard 中有效,但在 iOS 模拟器 (Xcode 8) 中无效