UICollectionView 正在选择多个单元格,即使多个单元格选择是错误的

UICollectionView is selecting multiple cell even multiple cell selection is false

我将 CollectionView 多单元格 selection 设置为 false 但是当我 select 一个单元格时它是 selecting 多单元格。我在那里看到了一些问题,但其中很多是针对 objective-c 的。我也试过 this way 但没用。

这是问题所在:

UICollectionViewDataSource 扩展代码:

    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()
            }
        }
    }
}

您应该为集合视图单元格使用“isSelected”方法。你这样做的方式只是选择单元格,无法取消选择它。此外,您将需要取消选择之前选择的单元格(如果有的话)。

所以我推荐使用这个方法:

首先,

        collectionView.allowsMultipleSelection = false

在 UICollectionViewCell 上,您可以覆盖 isSelected 方法

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)
            }
        }

最后,当您需要找出所选项目的索引路径时。

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