Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dequeueReusableCellWithReuseIdentifier crash 'could not dequeue a view of kind" UICollectionElementKindCell'

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.

like image 969
Ken Roy Avatar asked Oct 21 '12 22:10

Ken Roy


3 Answers

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];
like image 115
Typo Johnson Avatar answered Oct 19 '22 23:10

Typo Johnson


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.

like image 38
Gorm Avatar answered Oct 19 '22 21:10

Gorm


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.

like image 1
Enrico Susatyo Avatar answered Oct 19 '22 23:10

Enrico Susatyo