gpt4 book ai didi

ios - CollectionViewCell 不持久

转载 作者:行者123 更新时间:2023-11-28 10:33:24 25 4
gpt4 key购买 nike

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

在第一个代码片段中,我有我的自定义单元格,它将相关变量“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)
}

最后一段代码显示 Collection View 在出队时更新每个单元格的“inCurrentMonth”变量。如果我在归还单元格之前打印单元格,它们显然具有正确的 incurrentmonth 值。不管怎样,这个信息什么时候会恢复到错误值?

编辑:在出队时更新时,标签上文本的值在 Collection View 中正确显示

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
}

最佳答案

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 创建单元格的新实例,而不是使用 func cellForItem(at: IndexPath) -> UICollectionViewCell? UICollectionView 方法>

关于ios - CollectionViewCell 不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54857408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com