Search code examples
iosswiftuicollectionviewuicollectionviewlayout

CollectionViewCell not persistent


I'm not entirely sure how to ask this question but here is the code I think is important... everything is hooked up and working (I have left out other design code on the cell to make question more readable)

in the first code snippet I have my custom cell which defaults the variable "inCurrentMonth" in question to False

class DateCollectionViewCell: UICollectionViewCell {


public var inCurrentMonth: Bool!

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

}

in the code snippet below I am printing the variable inCurrentMonth for each of the cells - THIS ALWAYS PRINTS FALSE

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

the final snippet of code shows the collection view updating each cell's "inCurrentMonth" variable when dequeing. If I print the cells before I return them they clearly have the correct value for incurrentmonth. When the heck does this info revert to the false value no matter what??

edit: The value for text on labels shows correctly in the collection view when updated while dequeing

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
    }

Solution

  • in the code snippet below I am printing the variable inCurrentMonth for each of the cells - THIS ALWAYS PRINTS FALSE

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

    dequeueReusableCell creates new instance of a cell, instead of this please use func cellForItem(at: IndexPath) -> UICollectionViewCell? method of UICollectionView