每次我尝试选择一个不可见的单元格时,都会出现异常:
let cell = collectionView.cellForItem(at: indexPath) as! CustomCell
这就是为什么我决定首先滚动到它以便它可见然后选择它的原因:
collectionView.scrollToItem(at: indexPath, at: [], animated: true)
let cell = collectionView.cellForItem(at: indexPath) as! CustomCell
但我仍然得到:
fatal error: unexpectedly found nil while unwrapping an Optional value
对于这一行:
let cell = collectionView.cellForItem(at: indexPath) as! CustomCell
有人可以帮我解决这个问题吗?
谢谢和问候!
最佳答案
我们在 UICollectionViewDelegate 中做了 select 方法。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//here you can access the cell
guard let cell = collectionView.cellForItem(at: indexPath) as?
CustomCell else { return }
//write your code here
}
关于Swift 3 - CollectionView 选择不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46689641/