ataberkturan
ataberkturan

Reputation: 45

UICollectionView is selecting multiple cell even multiple cell selection is false

I set the CollectionView multiple cell selection as false but when I select one cell it is selecting multiple cell. I saw some questions in there but lots of them for objective-c. I also tried this way but it didn't work.

Here is the problem:

gif

UICollectionViewDataSource Extension Code:

    extension ThemesVC: UICollectionViewDataSource{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return themeManager.imageKeys.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ThemesCollectionViewCell.reuseID, for: indexPath) as? ThemesCollectionViewCell else {
            fatalError("Unexpected cell class dequeued")
        }
        
        cell.currentIndexPath = indexPath
        
        // Marking the selected theme as a default
        if ThemeManager.current.themeName == themeManager.themeNames[indexPath.row].themeName {
            cell.toggleSelect()
        }
        
        themeManager.fetchImage(atIndex: indexPath.item) { [weak cell] image, itemIndex in
            guard let cell = cell, let image = image else { return }
            DispatchQueue.main.async {
                guard let cellIndexPath = cell.currentIndexPath, cellIndexPath.item == itemIndex else {
                    return
                }
                cell.themeCellImageView.image = image
            }
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // Connecting with reusable cell
        if let cell = collectionView.cellForItem(at: indexPath) as? ThemesCollectionViewCell {
            if indexPath.row > 1{
                
                // Changing theme based on selected cell
                
                // Saving the selected theme value
                cell.saveData(value: indexPath.row)
                
                cell.toggleSelect()
                
              
                
            } else {
                // First index (0) cell DidSelect Function
                if indexPath.row == 0 {
                    
                    performSegue(withIdentifier: "toCreateTheme", sender: nil)
                    
                } else {
                    // Second index (1) cell DidSelect Function
                    
                    // Generating the random integer for Random theme selection
                    let randomIndex = Int.random(in: 2...41)
                    
                    // Saving the selected theme value
                    cell.saveData(value: randomIndex)
                }
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        // Connecting with reusable cell
       if let cell = collectionView.cellForItem(at: indexPath) as? ThemesCollectionViewCell {
            if indexPath.row > 1{
                cell.toggleSelect()
            }
        }
    }
}

Upvotes: 1

Views: 869

Answers (1)

Nitanta Adhikari
Nitanta Adhikari

Reputation: 128

You should make use of the "isSelected" method for the collection view cell. The way you are doing it is only selecting the cell there is no way of unselecting it. Also, you will need to unselect the previous selected cell If there are any.

So I recommend using this method:

Firstly,

        collectionView.allowsMultipleSelection = false

on the UICollectionViewCell you can override the isSelected method

didSet {
            if isSelected {
                 // Change UI for selected state
                radioButton.setImage(#imageLiteral(resourceName: "greenTick"), for: .normal)
            } else {
                // Chage UI for unselected state
                radioButton.setImage(#imageLiteral(resourceName: "radioInactive"), for: .normal)
            }
        }

Finally when you need to find out the indexpath for the selected item.

            guard let selectedIndex = self.collectionView.indexPathsForSelectedItems?.first else { return }

Upvotes: 1

Related Questions