gpt4 book ai didi

ios - 单击在数组中添加/删除时,Swift Collection View didSelectItemAtIndexPath

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

我有 swift collection view 和 items ,我想将单击的每个项目添加到数组中,再次单击时相同的项目将在数组中删除。我的代码在这里。

我的数组

var services = [""]

我的 didSelectItemAtIndexPath 代码

   func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {


let clickeditem = items[indexPath.section].services[indexPath.item - 1]
print(clickeditem) // here clicked item this item will be add to services array when first click, after clicked again will be remove in services array


}

clickeditem // will be add to services array when first click, after clicked again will be remove in services array

最佳答案

你可以使用结构,所以:

struct Item {
let indexPath: NSIndexPath
let value: AnyObject
}

services.append(Item(indexPath, value: clickeditem))

然后要删除,您可以迭代并检查具有该 indexPath 的元素:

for item in services {
if item.indexPath == indexPath {
//remove your item and whatever you want
}
}

关于ios - 单击在数组中添加/删除时,Swift Collection View didSelectItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38991183/

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