14

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.

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Ken Roy
  • 915
  • 1
  • 10
  • 15
  • 4
    Put a breakpoint at the `registerClass:forCellWithReuseIdentifier:` line. When it's hit, print `self.collectionView`. Is it nil? If so, you forgot to connect the outlet (if using a nib) or set the property (if using code). If it's not nil, put a breakpoint at the `dequeueReusableCellWithReuseIdentifier:forIndexPath:` line. Print `collectionView` when the breakpoint is hit. Do you get the same pointer that you got from the first breakpoint? – rob mayoff Oct 21 '12 at 22:57
  • @robmayoff Followed your steps, before `dequeueReusableCellWithReuseIdentifier:forIndexPath` it is has same pointer. But when it hits `dequeueReusableCellWithReuseIdentifier:forIndexPath` it crashes with the same error. – rohan-patel Jan 31 '14 at 21:34

4 Answers4

15

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];
Typo Johnson
  • 5,974
  • 6
  • 29
  • 40
4

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.

Gorm
  • 2,714
  • 1
  • 18
  • 12
1

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.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
0

Once when I was working on collection view cells, I wrote:

static NSString *identifier = @"cell "; 

Looking carefully, you will see an extra space at the end of the identifier string. After realizing my mistake, I hope this will be helpful to someone.

Reference:

could not dequeue a view of kind: UICollectionElementKindCell with identifier

Community
  • 1
  • 1
Narasimha Nallamsetty
  • 1,215
  • 14
  • 16