[英]CollectionView scrolling changes the selected cells
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
}
}
你可以試試
var selectedIndex = 0 // set default or nil and check it's nullability before usage
//
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 indexPath.row == selectedIndex {
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
}
我的CollectionView的didSelecteItemAt函數:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndex = indexPath.row
collectionview.reloadData()
}
我的CollectionView的didDeSelecteItemAt函數:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
// remove this
}
您可以通過將所有選定項的索引存儲在單獨的數組中,也可以管理單獨的屬性(如字典中的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()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.