I'm trying to setup an UICollectionView. At first I was using the basic UICollectionViewCell, it was working well, the cells were being displayed.
Then I created my own collectionViewCell (subclassing UICollectionViewCell), to be able to display an image in the cell.
I linked the custom cell in the Storyboard with an identifier and I also specified the right class for it.
However, I keep having this error, and I don't understand why:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MediaCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I managed to take care of the bug by adding this line in the viewDidLoad but in the tutorials I was following the guys were not using it:
[self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"MediaCell"];
The problem is that if I add it, the collectionView remains black and no data is displayed.
I had this exact issue yesterday (as the documentation says you MUST register the class), but the documentation is inaccurate in that you should not register if a prototype cell exists in your storyboard and you give it an identifier.
If you create a prototype of the UICollectionViewCell in your storyboard and assign your custom cell class, it is not necessary (and is detrimental if you do) register the custom UICollectionViewCell. This is the reason the resulting UICollectionView is black.
Based on the error message, the prototype cell in the storyboard does not have its identifier set correctly (either not set at all or is not MediaCell)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With