Ios 为什么在以编程方式选择单元格后-单元格不是交互?

Ios 为什么在以编程方式选择单元格后-单元格不是交互?,ios,swift,uitableview,uicollectionview,uikit,Ios,Swift,Uitableview,Uicollectionview,Uikit,我在以编程方式选择的collectionView中设置了一个单元格: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(with: FAFavouriteCategoryCell.self, fo

我在以编程方式选择的collectionView中设置了一个单元格:

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(with: FAFavouriteCategoryCell.self, for: indexPath)!
        let currentCategory = categories[indexPath.row]
        cell.isSelected = selectedCategories.contains(currentCategory.categoryID)
        cell.configCell(category: currentCategory)
        return cell
    }
在此之后,所有选定的单元格均不工作。我不能点击它。
如果我单击了method
shouldldeselectItemat
不调用

,您需要像这样调用
selectItem

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(with: FAFavouriteCategoryCell.self, for: indexPath)!
        let currentCategory = categories[indexPath.row]
        if selectedCategories.contains(currentCategory.categoryID) {
            collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .left) //Add this line
            cell.isSelected = true
        } else {
            collectionView.deselectItem(at: indexPath, animated: false)
            cell.isSelected = false
        }
        cell.configCell(category: currentCategory)
        return cell
    }

您应该调用
collectionView.selectItem(在indexPath:indexPath,动画:false,滚动位置:.none)
而不是
cell.isSelected
@Larme,我需要在init table时刻选择它。我该怎么称呼它?很好。这是可行的,但在第一次点击后(我需要在初始时刻显示所选项目),您不应该在else(非书面)中调用DeceletItem以避免重用潜在问题吗?isSelected是否有必要?