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

Ken picture Ken · Oct 22, 2012 · Viewed 29.2k times · Source

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.

Answer

Typo Johnson picture Typo Johnson · Oct 26, 2013

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];