Koen Koen - 11 months ago 112
Swift Question

collectionView.cellForItem crashes

i'm using collectionView.cellForItem in the funciton call:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

let cell = collectionView.cellForItem(at: indexPath) as! PlaybookPlayCell

That is working fine. This tells me that the collection view setup is done correctly.
BUT if i use collectionView.cellForItem(at: indexPath) the function call sizeForItemAt i get a crash on the assignment of the cell

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

let cell = collectionView.cellForItem(at: indexPath) as! PlaybookPlayCell // CRASH ON THIS LINE


return CGSize(width: ...., height: ......)

Would be great to get some input.

Answer Source

It will be always nil because collectionView.cellForItem(at: indexPath) will be called before initialization of collection view(collectionView.cellForRaw(at: indexPath)). Set the breakpoints in both of them and you will see.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download