我对collectionView
有一个奇怪的问题,我有两个原型(prototype),第一个应该在indexPath.row=0
中绘制,第二个应该创建四次。
第二个原型(prototype)包含一个 UIView,用于保存使用 PaintCode 应用程序创建的图标和一个文本标签。每个单元格还有四个图标。
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch indexPath.row {
case 0:
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as? ViewData {
return cell
}
case 1,2,3,4:
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as? ViewData {
switch indexPath.row {
case 1:
cell.dataIcon.is1stIcon = true
cell.title.text = "1"
case 2:
cell.dataIcon.is2ndIcon = true
cell.title.text = "2"
case 3:
cell.dataIcon.is3rdIcon = true
cell.title.text = "3"
case 4:
cell.dataIcon.is4thIcon = true
cell.title.text = "4"
default:
break
}
return cell
}
default:
break
}
return UICollectionViewCell()
}
问题是,所有文本都位于正确的位置(行),但 indexPath.row=1
中的 1stIcon
将显示在 中indexPath.row=3
和 3rtIcon
将显示在 indexPath.row=1
中。
首先,我认为问题可能出在图标上,但无论我在这些行中放入什么,都具有相同的错误倾向。
你对此有何看法?
谢谢
最佳答案
问题发生在单元出队时,您需要在使用它之前重新设置所有内容
cell.reset()
switch indexPath.row {
case 1:
cell.dataIcon.is1stIcon = true
cell.title.text = "1"
case 2:
cell.dataIcon.is2ndIcon = true
cell.title.text = "2"
case 3:
cell.dataIcon.is3rdIcon = true
cell.title.text = "3"
case 4:
cell.dataIcon.is4thIcon = true
cell.title.text = "4"
default:
break
}
func reset() {
// show all of them
self.dataIcon.is1ndIcon = false
self.dataIcon.is2ndIcon = false
self.dataIcon.is3ndIcon = false
self.dataIcon.is4ndIcon = false
}
关于ios - 在 UICollectionView 中设置排序属性的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56266467/