gpt4 book ai didi

ios - CollectionView 滚动更改选定的单元格

转载 作者:行者123 更新时间:2023-11-30 11:24:31 24 4
gpt4 key购买 nike

CollectionView 滚动会更改选定的单元格。例如,我的collectionView中有26个字母,items[0]是A,items[1]是B,...items[25]是Z,现在我选择了items[1],它是B,当我滚动collectionView,items[1]将被取消选择,并且将选择另一个项目。我知道问题是由重用引起的,但我不知道如何正确修复它,即使我使用了 google 或 StackOverflow 中显示的一些解决方案。

我的viewDidLoad:

collectionView.allowsMultipleSelection = false

我的 CollectionViewCell 类:

import UIKit
class AddCollectionViewCell: UICollectionViewCell {
}

我的 CollectionView 设置:

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return iconSet.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "iconCell", for: indexPath) as! IconCollectionViewCell
cell.iconImage.image = UIImage(named:iconSet[indexPath.row])
return cell

}

我的 CollectionView 的 didSelecteItemAt 函数:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

if let cell = collectionView.cellForItem(at: indexPath) {
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.3)
cell.backgroundView?.layer.cornerRadius = 5
}

我的 CollectionView 的 didDeSelecteItemAt 函数:

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

if let cell = collectionView.cellForItem(at: indexPath) {
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.0)
cell.backgroundView?.layer.cornerRadius = 5
}
}

最佳答案

您可以通过将所有选定项的索引存储在单独的数组中来实现此目的,也可以在字典或自定义类对象数组中管理单独的属性,例如 isSelected(无论您用于填充 CollectionView 单元格)。

您可以简单地使用数组,如下所示:var arrIndex : [Int] = Int

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "iconCell", for: indexPath) as! IconCollectionViewCell
cell.iconImage.image = UIImage(named:iconSet[indexPath.row])

if arrIndex.contains(indexPath.item) {
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.3)

}
else {
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.0)

}
return cell

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

if !arrIndex.contains(indexPath.item) {
arrIndex.append(indexPath.item)
collectionview.reloadData()

}
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if arrIndex.contains(indexPath.item) {
arrIndex = arrIndex.filter { $0 != indexPath.item }
collectionview.reloadData()
}
}

关于ios - CollectionView 滚动更改选定的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50913785/

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