首页 > 解决方案 > 在 UICollectionView 中点击单元格返回错误的索引

问题描述

我真的不知道我错过了什么。我有一个UICollectionView设置,内容设置正确。该cellForItemAt部分如下所示:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DetailsEventImageCell", for: indexPath) as? DetailsEventImageCell
    
    print("Loaded - IndexPathItem: \(indexPath.item)")
    
    cell?.backgroundColor = .clear
    cell?.contentView.layer.cornerRadius = 10
    cell?.contentView.layer.masksToBounds = true
    cell?.eventImage.image = images[indexPath.row]
    
    return cell ?? UICollectionViewCell()
}

正确显示collectionView单元格,但这是问题所在。当我进入编辑模式并随机点击所有单元格时,有时它会返回错误的索引并标记错误的单元格(主要是在它旁边),如下图所示:

我点击的单元格应该返回 indexPath.item = 5 但它确实返回 4 并将单元格 4 标记为删除而不是 5。

由于单元格设置正确,我不知道为什么它偶尔会indexPath为所选单元格返回错误。如果我点击一个单元格两次,它会突然返回正确的indexPath. 如果我取消选择所有单元格并重试,则某些单元格会indexPath再次返回错误。

这是我的didSelectItemAt代码

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath.item)
    if isEditing {
        if let cell = collectionView.cellForItem(at: indexPath) as? DetailsEventImageCell {
            cell.isInEditingMode = isEditing
            cell.isSelected = true
        }
    }
}

以下两个函数也可能是罪魁祸首,但我只是不明白为什么它会collectionView正确设置然后点击返回错误值。

这是我的自定义单元格:

class DetailsEventImageCell: UICollectionViewCell {

@IBOutlet weak var eventImage: UIImageView!
@IBOutlet weak var selectedForDeletionImage: UIImageView!

var isInEditingMode: Bool = false {
    didSet {
        if !isInEditingMode {
            //selectedForDeletionImage.isHidden = true
            isSelected = false
        }
    }
}

override var isSelected: Bool {
    didSet {
        if isInEditingMode {
            selectedForDeletionImage.isHidden = isSelected ? false : true
        }
    }
}

这是我的setEditing代码

override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        
        collectionView.allowsMultipleSelection = editing
        
        //Deselect all selected cells
        if !editing {
            if let indexPaths = collectionView.indexPathsForSelectedItems {
                for indexPath in indexPaths {
                    self.collectionView.deselectItem(at: indexPath, animated: false)
                }
            }
        }
        
        
        let indexPaths = collectionView.indexPathsForVisibleItems
        for indexPath in indexPaths {
            if let cell = collectionView.cellForItem(at: indexPath) as? DetailsEventImageCell {
                cell.isInEditingMode = editing
            }
        }
    }

我希望有人可以帮助我。

标签: iosswiftxcodeuicollectionview

解决方案


经过数小时的尝试,我发现这似乎是与 iOS 模拟器相关的问题。我无法访问所有设备尺寸,所以我有时会使用它进行快速测试。

在模拟器中“缓慢”点击单元格确实可以按预期工作,但如果我在单元格内点击得太快,它似乎没有跟上并再次选择前一个单元格。

在实际设备上,可以以任何速度点击单元格,并且可以按预期工作。

编辑:这个问题似乎只出现在我的 Mac Mini 2012 年末(macOS Catalina 10.15.6)上的 iOS 模拟器上 XCode 11.7 我的 Macbook Pro 15" 2019 和我尝试过的所有硬件设备(iPhone 11 Pro、ipad Air、iPhone SE 2nd Gen)在相同的项目和相同的 OS/XCode 构建中按预期工作。


推荐阅读