16

I get the following error:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier FontCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

I'm not sure exactly what I'm doing wrong. I set the cell identifier (programmatically, as it's not created through Interface Builder) and do everything I thought I was supposed to do in the delegate methods, but I'm still getting that error when I try to get the UITableView to load.

Here's the relevant code (it's worth noting I've subclassed UITableViewCell for customization options):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.fonts.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"FontCell";

    FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (!cell) {
        cell = [[FontCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FontCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    int row = indexPath.row;

    cell.fontFamilyLabel.text = self.fonts[row];

    return cell;
}

And here's the only method I changed in my subclassed UITableViewCell (FontCell):

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.fontFamilyLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
        self.fontFamilyLabel.textAlignment = NSTextAlignmentCenter;

        [self.contentView addSubview:self.fontFamilyLabel];
    }
    return self;
}

What exactly am I doing wrong?

Brian
  • 15,599
  • 4
  • 46
  • 63
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • I found this thread it has a good solution: http://stackoverflow.com/questions/12737860/assertion-failure-in-dequeuereusablecellwithidentifierforindexpath – Khaled Annajar Aug 27 '13 at 22:55

4 Answers4

27

Easiest fix is to just change it to FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; As with your current code, you'll have to check to be sure cell is not nil if you do this method.


Alternately, you can register a UINib or Class at the table level that is tied to @"FontCell"

For example (up in viewDidLoad):

[self.tableView registerClass: [FontCell class] forCellReuseIdentifier:@"FontCell"];

Then you can do

FontCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FontCell" forIndexPath:indexPath];

The nice thing with this method is that you know that your cell will never be nil, so you can just immediately begin modifying it.

Natan R.
  • 5,141
  • 1
  • 31
  • 48
cscott530
  • 1,658
  • 16
  • 25
4

You're using the dequeueReusableCellWithIdentifier:forIndexPath: method. The documentation for that method says this:

You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

So here

[self.tableView registerClass: [FontCell class] forReuseIdentifier: @"FontCell"];
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
2

I also had such a problem, and the solution I found is:

Go to the Project Navigator and select “ViewController.h”. Append

 <UITableViewDelegate, UITableViewDataSource>

after “UIViewController”.

offset
  • 1,407
  • 1
  • 16
  • 34
1

If using a tableview (not a table view controller) as I was, there are not cells that appear by default.

In the storyboard select the tableview Open the Attributes inspector Change prototype cells from 0 to 1 Select the newly displayed table cell In the Attributes inspector set the Identitifier to "FontCell"

masteroleary
  • 1,014
  • 2
  • 16
  • 33