ios - 为什么以编程方式选择单元格后 - 单元格不是交互?
问题描述
我在以编程方式选择的 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
}
在此之后所有选定的单元格都不起作用。我无法点击它。如果我点击方法shouldDeselectItemAt
不调用
解决方案
你需要这样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
}