![](/img/trans.png)
[英]Programmatically selecting UICollectionView cell which are not loaded yet
[英]Why after selecting cell programmatically - cell is not interaction?
我在以编程方式选择的 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.