CollectionViewCell 不持久
CollectionViewCell not persistent
我不完全确定如何提出这个问题,但这是我认为重要的代码...一切都已连接并正常工作(我在单元格上省略了其他设计代码以使问题更具可读性)
在第一个代码片段中,我有我的自定义单元格,它默认变量 "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" 变量。如果我在 return 之前打印单元格,它们显然具有正确的 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
}
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
创建一个单元格的新实例,而不是这个请使用 UICollectionView
的 func cellForItem(at: IndexPath) -> UICollectionViewCell?
方法
我不完全确定如何提出这个问题,但这是我认为重要的代码...一切都已连接并正常工作(我在单元格上省略了其他设计代码以使问题更具可读性)
在第一个代码片段中,我有我的自定义单元格,它默认变量 "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" 变量。如果我在 return 之前打印单元格,它们显然具有正确的 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
}
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
创建一个单元格的新实例,而不是这个请使用 UICollectionView
func cellForItem(at: IndexPath) -> UICollectionViewCell?
方法