Taking first plunge with collection views and am running into this error:
Terminating app due to uncaught exception \'NSInternalInconsistencyException
i had everything 100% done correctly. but for some reason i got the same error for an hour, then what i did is:
simply, redid this step even tho i made it correct before, its like xcode misses something at a specific nanosecond!
You need to use same identifier between the dequeueReusableCellWithReuseIdentifier's argument and the UICollectionViewCell's property.
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
You need to give correct reuseble identifier in storyboard, which you have give in the code while registering the collectionViewCell.
Here is the code.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ClctnVC", for: indexPath)as! ClctnVC
return cell
}
Complementing what @jrtuton written... What you need is:
1) Register your nib file to "connect" it with your identifier:
//MyCollectionView.m
- (void) awakeFromNib{
[self registerNib:[UINib nibWithNibName:@"NibFileName" bundle:nil] forCellWithReuseIdentifier: @"MyCellIdentifier"];
}
2) Use your identifier to load your custom cell from a nib:
//MyCollectionView.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCellIdentifier" forIndexPath:indexPath];
}
3) Use always static NSString* to avoid the same identifiers again in your app.
What fixed this for me was (roughly) same as Omar's answer, except I ended up creating a new UICollectionViewCell custom class and giving the Cell Reuse Indentifier a new / different name than the one that I had used elsewhere in the application. It worked immediately after that.
Just in case, if you are working with storyboard set collectionView identifier in the right place in the Attributes Inspector -> Identifier field. Not under the class name in "Restoration ID".
If you are using collection view in tableView cell, add delegates to tableView cell not in the tableViewController.