【问题标题】:UICollectionView is selecting multiple cell even multiple cell selection is falseUICollectionView 正在选择多个单元格,即使多个单元格选择是错误的
【发布时间】:2021-09-02 20:24:22
【问题描述】:

我将 CollectionView 的多个单元格选择设置为 false,但是当我选择一个单元格时,它正在选择多个单元格。我在那里看到了一些问题,但其中很多是针对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()
            }
        }
    }
}

【问题讨论】:

  • 添加 else 部分并设置您的 toggleUnSelect 方法。

标签: swift uicollectionview


【解决方案1】:

您应该对集合视图单元格使用“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 }

【讨论】:

    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多