gpt4 book ai didi

swift - 尝试删除多个(以编程方式添加的)单元格后 CollectionView 崩溃

转载 作者:行者123 更新时间:2023-11-28 15:47:41 25 4
gpt4 key购买 nike

我有一个带有非常简单数据源的 UICollectionView。它只是一个以编程方式更新的字符串数组。使用此 UICollectionView,我可以点击以选择多个单元格 - 然后可以使用删除按钮删除所有选定的单元格。很简单,除了我有时会遇到 fatal error :索引超出范围。

通过在另一个名为“All Hobbies”的 Collection View 中点击单元格,以编程方式添加单元格 - 在“All Hobbies”CV 中点击和选择的任何单元格都会添加到“My Hobbies”中。我通过将点击的单元格的值附加到控制 My Hobbies CV 的数组来实现这一点。

问题

  1. 根据我在“我的爱好”中选择的单元格,删除按钮(下面的代码)有时会删除它们,或者有时会崩溃并出现 fatal error :索引超出范围。它似乎不会在第一次删除时崩溃(如果有帮助的话。)
  2. 如果“我的爱好”中的单元格确实删除了,当我点击“所有爱好”中的更多单元格时 - 它们将作为 UIColor.red 添加到“我的爱好”(表示它们已被选中。)(然后稍后会崩溃我按下删除按钮。)单元格应添加为黄色。

似乎我的数据在删除后没有更新,当我再次尝试删除时,索引超出了范围。但是我在我的代码中找不到导致这种情况发生的问题(如果这确实是问题......)

类(class)

let allHobbiesArray = ["Skiing", "Jogging", "Bicycling", "Basketball", "Cricket", "Being a Couch Potato", "Skiing", "Jogging", "Bicycling", "Basketball", "Cricket", "Being a Couch Potato", "Skiing", "Jogging", "Bicycling", "Basketball", "Cricket", "Being a Couch Potato", "Skiing", "Jogging", "Bicycling", "Basketball", "Cricket", "Being a Couch Potato"]
var myHobbiesArray = [String]()
var allSelected = [IndexPath]()

数据源

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == allHobbiesCV {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ALL", for: indexPath) as! AllHobbiesCell
cell.allHobbiesLabel.text = allHobbiesArray[indexPath.row]
return cell
}
else {
let cell = myHobbiesCV.dequeueReusableCell(withReuseIdentifier: "MY", for: indexPath) as! MyHobbiesCell
cell.myHobbiesLabel.text = myHobbiesArray[indexPath.row]
return cell
}
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == allHobbiesCV {
return allHobbiesArray.count
}
else {
return myHobbiesArray.count
}
}

委托(delegate)

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

if collectionView == allHobbiesCV {
let cell = allHobbiesCV.cellForItem(at: indexPath) as! AllHobbiesCell

myHobbiesArray.append(allHobbiesArray[indexPath.row])

myHobbiesCV.reloadData()
for i in 0..<allSelected.count {
self.myHobbiesCV.selectItem(at: allSelected[i], animated: false, scrollPosition: [])
}
}

else {
let cell = myHobbiesCV.cellForItem(at: indexPath) as! MyHobbiesCell
cell.backgroundColor = UIColor.red // Red background to show that it's selected

// Store all of the selected cells in allSelected
allSelected = self.myHobbiesCV.indexPathsForSelectedItems!

}

}

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

if collectionView == allHobbiesCV {
let cell = allHobbiesCV.cellForItem(at: indexPath) as! AllHobbiesCell
cell.backgroundColor = UIColor.yellow
}
else {
let cell = myHobbiesCV.cellForItem(at: indexPath) as! MyHobbiesCell
cell.backgroundColor = UIColor.yellow

// update allSelected after deselect
allSelected = self.myHobbiesCV.indexPathsForSelectedItems!

}

}

删除单元格

@IBAction func deleteButtonPressed(_ sender: UIButton) {
if allSelected.count > 0 {
for element in allSelected {
removeSelectedCells(i: element.item)
}
}
}

func removeSelectedCells(i: Int) {
self.myHobbiesArray.remove(at: i) // This is what Xcode shows as the crash on the fatal error.
let indexPath = NSIndexPath(row: i, section: 0)

self.myHobbiesCV.deleteItems(at: NSArray(object: indexPath) as! [IndexPath])

//self.myHobbiesCV.performBatchUpdates({
// self.myHobbiesCV.deleteItems(at: NSArray(object: indexPath) as! [IndexPath])
//}) { (true) in
// self.myHobbiesCV.reloadData()
//}
}

非常感谢任何帮助来处理从 Collection View 中删除多个单元格。如果您需要更多信息,我很乐意澄清。

最佳答案

使用 SO 答案 here ,您应该能够从 indexPaths 的 allSelected 数组中获取索引,然后一次性从 myHobbiesArray 中删除索引。

@IBAction func deleteButtonPressed(_ sender: UIButton) {
let indices = allSelected.map{ $0.item }
myHobbiesArray = myHobbiesArray.enumerated().flatMap { indices.contains($0.0) ? nil : $0.1 }
self.myHobbiesCV.deleteItems(at: allSelected)
}

关于swift - 尝试删除多个(以编程方式添加的)单元格后 CollectionView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42775917/

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