Search code examples
iosswiftuicollectionviewuicollectionviewcelljquery-ui-selectable

How to select all UICollectionView Cell's and Deselect by button click?


I need realize some filer with over 15 buttons as filter tabs, I added horizontal CollectionView, custom Cell, two array’s. Single selection works well, but I need select all and deselect all by button click any idea how to realize it?

Selecting all the items in UICollectionView iOS, even the cells that are not visible

this one doesn't work


 var arraySelectedFilterIndex = [IndexPath]()
 var arraySelectedFilterData = [String]()

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.filterTitles.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterCell", for: indexPath) as! FilterCell

        cell.titleL.text = String(filterTitles[indexPath.row])
        cell.pointIMG.image = UIImage(named: filterTitles[indexPath.row] + "40")

        if arraySelectedFilterIndex.contains(indexPath) { // You need to check wether selected index array contain current index if yes then change the color
            cell.isSelected = true
        }
        else {
            cell.isSelected = false
        }

        cell.layoutSubviews()
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.item)!")
        Haptico.shared().generate(.light)

        let strData = filterTitles[indexPath.item]

        if arraySelectedFilterIndex.contains(indexPath) {
            arraySelectedFilterIndex = arraySelectedFilterIndex.filter { $0 != indexPath}
            arraySelectedFilterData = arraySelectedFilterData.filter { $0 != strData}
        }
        else {
            arraySelectedFilterIndex.append(indexPath)
            arraySelectedFilterData.append(strData)
        }
        collectionView.reloadData()
        print(arraySelectedFilterData)
    }

@IBAction func selectAllA(_ sender: Any) {. 

//here need add code with All Select and Deselect functions

}


Solution

  • I'm not sure why sometimes you use "indexPath.row" and sometimes "indexPath.item".

    Regardless, your function should look something like this

    @IBAction func selectAllA(_ sender: Any) {
            arraySelectedFilterIndex.removeAll()
            arraySelectedFilterData.removeAll()
    
            for (index, element) in self.filterTitles.enumerated() {
                arraySelectedFilterIndex.append(IndexPath(item: index, section: 0))
                arraySelectedFilterData.append(element)
            }
    
            collectionView.reloadData()
            print(arraySelectedFilterData)
        }
    

    At first you are clearing previous selections to avoid duplicates, you could use a map/dictionary structure to avoid duplicated instead of a list though.

    Then for each element you add the index and data to the selected list. Finally reload the data, I kept you the print at the end so you can check it.