【问题标题】:CollectionViewCell not persistentCollectionViewCell 不持久
【发布时间】:2019-02-24 22:59:36
【问题描述】:

我不完全确定如何提出这个问题,但这是我认为很重要的代码......一切都已连接并正常工作(我在单元格上省略了其他设计代码以使问题更具可读性)

在第一个代码 sn-p 中,我有我的自定义单元格,它将变量“inCurrentMonth”默认为 False

class DateCollectionViewCell: UICollectionViewCell {


public var inCurrentMonth: Bool!

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

}

在下面的代码 sn-p 中,我正在为每个单元格打印变量 inCurrentMonth - 这总是打印错误

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

最后的 sn-p 代码显示了在 dequeing 时更新每个单元格的“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
    }

【问题讨论】:

  • 离题:请避免隐式展开的属性类型 (Bool!)。
  • @J.Doe 你有什么推荐的视频或快速阅读吗?或者只是苹果文档?毕业后在 swift 开始全职编码之前,最好先复习一下。谢谢!

标签: ios swift uicollectionview uicollectionviewlayout


【解决方案1】:

在下面的代码 sn-p 中,我正在打印变量 inCurrentMonth 对于每个单元格 - 这总是打印错误

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) -&gt; UICollectionViewCell?func cellForItem(at: IndexPath) -&gt; UICollectionViewCell? 方法来代替UICollectionView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    相关资源
    最近更新 更多