[英]Swift Collection view didSelectItemAtIndexPath when clicked add / remove in array
I have swift collection view and have items , i want to add each item clicked to array and same item when clicked again will be remove in array. 我有快速收集视图并有项目,我想将单击的每个项目添加到数组,再次单击相同的项目将在数组中删除。 My codes here.
我的代码在这里。
My array 我的数组
var services = [""]
My didSelectItemAtIndexPath codes 我的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
clickeditem //将在第一次单击时添加到服务数组,再次单击后将在服务数组中删除
You could use a struct, so: 您可以使用结构,因此:
struct Item {
let indexPath: NSIndexPath
let value: AnyObject
}
services.append(Item(indexPath, value: clickeditem))
and then to remove you can iterate and check the element with that indexPath: 然后要删除,您可以迭代并检查带有该indexPath的元素:
for item in services {
if item.indexPath == indexPath {
//remove your item and whatever you want
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.