gpt4 book ai didi

ios - Swift - 切换 CollectionView 单元格以切换数组中的项目

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

我有两个 CollectionView。一个 CollectionView (allHobbiesCV) 预先填充了您可以选择的兴趣爱好。另一个 CollectionView (myHobbiesCV) 是空的,但如果您在 allHobbiesCV 中点击一个爱好,它就会被添加到 myHobbiesCV 中。这一切都很好。

我希望点击的 allHobbiesCV 单元格切换到选中状态,它将爱好添加到 myHobbiesCV,然后如果用户在 allHobbiesCV 中再次点击相同的选定单元格,它会从 myHobbiesCV 中删除该爱好。基本上是一个切换添加/删除。

需要注意两点:

  1. 用户可以在 myHobbiesCV 中手动选择爱好,然后点击 [删除爱好] 按钮。
  2. Hobbies 将按季节排序,因此 allHobbiesArray 将有 4 个不同的数据集(冬季、 Spring 、夏季、秋季)。取决于用户点击的季节 (ViewController)。他们可以根据需要从每个单元格中选择任意数量的单元格。

问题:

除了第一个单元格之外,我在任何切换时都会崩溃。如果我选择 allHobbiesCV 中的第一个单元格,我可以再次选择它,它会将它从 myHobbiesCV 中删除。如果我再次选择同一个单元格(切换它),我会崩溃。如果我选择第一个单元格以外的任何其他单元格,我会崩溃。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete item 7 from section 0 which only contains 3 items before the update'

类(class)

// Winter Hobbies
let allHobbiesArray = ["Skiing", "Snowboarding", "Drinking Bourbon", "Snow Shoeing", "Snowmobiling", "Sledding", "Shoveling Snow", "Ice Skating"]
var myHobbiesArray = [String]()
var allSelected = [IndexPath]()
var didSelectIPArray = [IndexPath]()

数据源

extension ViewController: UICollectionViewDataSource {
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.item]

if cell.isSelected {
cell.backgroundColor = UIColor.green
}
else {
cell.backgroundColor = UIColor.yellow
}

return cell
}

else {
let cell = myHobbiesCV.dequeueReusableCell(withReuseIdentifier: "MY", for: indexPath) as! MyHobbiesCell
cell.myHobbiesLabel.text = myHobbiesArray[indexPath.item]

if cell.isSelected {
cell.backgroundColor = UIColor.red
}
else {
cell.backgroundColor = UIColor.yellow
}


return cell
}


}

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

}

}

委托(delegate)

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


if collectionView == allHobbiesCV {
if myHobbiesArray.count <= 6
self.allSelected = []

didSelectIPArray.append(indexPath) // Store the selected indexPath in didSelectIPArray

myHobbiesArray.insert(allHobbiesArray[indexPath.item], at: 0)
let allHobbiesCell = allHobbiesCV.cellForItem(at: indexPath) as! AllHobbiesCell
allHobbiesCell.backgroundColor = UIColor.green

// Store all of the selected cells in an array
didSelectIPArray.append(indexPath)

myHobbiesCV.reloadData()
}
}


else {
let cell = myHobbiesCV.cellForItem(at: indexPath) as! MyHobbiesCell
cell.backgroundColor = UIColor.red
allSelected = self.myHobbiesCV.indexPathsForSelectedItems!
}


}

// Deselecting selected cells
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

if collectionView == allHobbiesCV {

// Yellow is the unselected cell color. So let's change it back to yellow.
let allHobbiesCell = allHobbiesCV.cellForItem(at: indexPath) as! AllHobbiesCell
allHobbiesCell.backgroundColor = UIColor.yellow

// Remove (toggle) the cells. This is where I am stuck/crashing.
let indices = didSelectIPArray.map{ $0.item }
myHobbiesArray = myHobbiesArray.enumerated().flatMap { indices.contains($0.0) ? nil : $0.1 }
self.myHobbiesCV.deleteItems(at: didSelectIPArray)

myHobbiesCV.reloadData()

didSelectIPArray.remove(at: indexPath.item) // Remove the deselected indexPath from didSelectIPArray
}

else { // MyHobbies CV
let cell = myHobbiesCV.cellForItem(at: indexPath) as! MyHobbiesCell
cell.backgroundColor = UIColor.yellow

// Store the selected cells to be manually deleted.
allSelected = self.myHobbiesCV.indexPathsForSelectedItems!

}
}
}

删除按钮

@IBAction func deleteButtonPressed(_ sender: UIButton) {
for item in didSelectIPArray {
self.allHobbiesCV.deselectItem(at: item, animated: false) // Try deselecting the deleted items in allHobbiesCV... This is crashing.
}
}

问题是我如何尝试在 didDeselect 中切换 allHobbiesCV 单元格。我在 didSelectIPArray 中保存所选单元格的方法是否正确?

如果需要,我很乐意提供进一步的见解。提前谢谢各位 friend !

最佳答案

不需要所有的 mapflatmap 东西。您可以简单地从选定的爱好数组中删除该对象:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == allHobbiesCV {
if myHobbiesArray.count <= 6
self.allSelected = []
myHobbiesArray.insert(allHobbiesArray[indexPath.item], at: 0)

let allHobbiesCell = allHobbiesCV.cellForItem(at: indexPath) as! AllHobbiesCell
allHobbiesCell.backgroundColor = UIColor.green

myHobbiesCV.insertItems(at: [IndexPath(item: 0, section: 0)])
}
} else {
let cell = myHobbiesCV.cellForItem(at: indexPath) as! MyHobbiesCell
cell.backgroundColor = UIColor.red
allSelected = self.myHobbiesCV.indexPathsForSelectedItems!
}
}

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

if collectionView == allHobbiesCV {

// Yellow is the unselected cell color. So let's change it back to yellow.
let allHobbiesCell = allHobbiesCV.cellForItem(at: indexPath) as! AllHobbiesCell
allHobbiesCell.backgroundColor = UIColor.yellow

let hobby = allHobbiesArray[indexPath.item]

if let index = myHobbiesArray.index(of:hobby) {
myHobbiesArray.remove(at: index)
myHobbiesCV.deleteItems(at: [IndexPath(item: index, section:0)])
}
} else { // MyHobbies CV
let cell = myHobbiesCV.cellForItem(at: indexPath) as! MyHobbiesCell
cell.backgroundColor = UIColor.yellow

// Store the selected cells to be manually deleted.
allSelected = self.myHobbiesCV.indexPathsForSelectedItems!

}
}

关于ios - Swift - 切换 CollectionView 单元格以切换数组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43057013/

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