I am getting the following crash:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I do have the following in my ViewDidLoad:
[self.collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:@"Cell"];
And the line that crashes is in the cellForItemAtIndexPath callback:
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
I've been searching for hours but cannot find any solution. I've tried subclassing the UICollectionViewCell but get the same error.
With breakpoints I have determined that the registerClass line is being executed before the dequeueReusableCellWithReuseIdentifier callback is executed.
I had this problem because I was calling registerClass before I was instantiating my table view object. Working code :
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
Also, don't forget the difference between a cell (register... forCellWithReuseIdentifier) and a supplementary view (register... forSupplementaryViewOfKind).
I was dequeing it correctly with the supplementary (header/footer) type, but I accidentally registered it as a cell type. Duh.
If you happen to experience this, in UICollectionViewController
or UITableViewController
, do as what rob mayoff said and make sure that your Collection View
or Table View
is hooked up property in your Storyboard.
Another common mistake is in the Storyboard you forgot to give the right CollectionID
or CellID
.
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