2016-08-17 67 views
0

我有快速集合視圖和項目,我想添加每個項目點擊數組和相同的項目時再次單擊將被刪除數組中。我的代碼在這裏。Swift集合視圖didSelectItemAtIndexPath單擊時添加/刪除數組

我的陣列

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 //將添加到服務陣列時,第一次點擊,點擊後會再次在服務數組中刪除

回答

1

你可以使用一個結構,所以:

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 
    } 
} 
相關問題