CollectionViewCell不是持久的

时间:2019-02-24 22:59:37

标签: ios swift uicollectionview uicollectionviewlayout

我不确定如何问这个问题,但这是我认为很重要的代码...一切都已完成并正常工作(我在单元格上省略了其他设计代码,以使问题更易于理解)< / p>

在第一个代码段中,我有一个自定义单元格,该单元格将有问题的变量“ inCurrentMonth”默认设置为False

class DateCollectionViewCell: UICollectionViewCell {


public var inCurrentMonth: Bool!

required init?(coder aDecoder: NSCoder) {
    super.init(coder:aDecoder)
    //You Code here
    inCurrentMonth = false
}

}

在下面的代码片段中,我为每个单元格打印变量inCurrentMonth-这总是打印为假

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Calendar", for: indexPath) as! DateCollectionViewCell
    print(cell.inCurrentMonth)
}

最后的代码片段显示了出库时更新每个单元格的“ inCurrentMonth”变量的集合视图。如果我在返回它们之前打印这些单元格,则它们显然具有当前月份的正确值。不管什么时候,此信息何时恢复为假值?

编辑:出队时更新时,标签上的文本值会正确显示在集合视图中

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Calendar", for: indexPath) as! DateCollectionViewCell

    switch Direction {  

    case 0:
        if indexPath.row < DaysInMonths[month] + NumberOfEmptyBox{
            cell.DateLabel.text = "\(indexPath.row + 1 - NumberOfEmptyBox)"
            cell.inCurrentMonth = true
        } else {
            cell.DateLabel.text = "\(indexPath.row - DaysInMonths[month] - NumberOfEmptyBox + 1)"
            cell.DateLabel.textColor = UIColor.lightGray
            cell.inCurrentMonth = false
        }
    case 1:
        if indexPath.row < DaysInMonths[month] + NextNumberOfEmptyBox{
            cell.DateLabel.text = "\(indexPath.row + 1 - NextNumberOfEmptyBox)"
            ?????????????THIS DOES WORK?????????????
            cell.inCurrentMonth = true
        } else {
            !!!!!!!!!!!!!THIS DOES NOT WORK!!!!!!!!!!!!!!!
            cell.DateLabel.text = "\(indexPath.row - DaysInMonths[month] - NextNumberOfEmptyBox + 1)"
            cell.DateLabel.textColor = UIColor.lightGray
            cell.inCurrentMonth = false
        }
    case -1:
        if indexPath.row < DaysInMonths[month] + PreviousNumberOfEmptyBox{
            ?????????????THIS DOES WORK?????????????
            cell.DateLabel.text = "\(indexPath.row + 1 - PreviousNumberOfEmptyBox)"
            !!!!!!!!!!!!!THIS DOES NOT WORK!!!!!!!!!!!!!!!
            cell.inCurrentMonth = true
        } else {
            cell.DateLabel.text = "\(indexPath.row - DaysInMonths[month] - PreviousNumberOfEmptyBox + 1)"
            cell.DateLabel.textColor = UIColor.lightGray
            cell.inCurrentMonth = false
        }
    default:
        fatalError()
    }

    if Int(cell.DateLabel.text!)! < 1 { //here we hide the negative numbers or zero

        var previousMonth: Int = month-1
        if previousMonth == -1 {
            previousMonth = 11
        }
        let previousDay: Int = DaysInMonths[previousMonth] - NumberOfEmptyBox + (indexPath.row) + 1
        cell.DateLabel.text = "\(previousDay)"
        cell.DateLabel.textColor = UIColor.lightGray
        cell.inCurrentMonth = false
    }

1 个答案:

答案 0 :(得分:3)

  

在下面的代码片段中,我在CurrentMonth中打印变量   每个单元格-这总是打印为假

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Calendar", for: indexPath) as! DateCollectionViewCell
    print(cell.inCurrentMonth)
}

dequeueReusableCell创建单元格的新实例,请使用func cellForItem(at: IndexPath) -> UICollectionViewCell?中的UICollectionView方法