Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting an error about being unable to dequeue when my UITableView tries to load?

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?

like image 714
Doug Smith Avatar asked Apr 02 '13 20:04

Doug Smith


People also ask

Could not dequeue cell with identifier?

…it means that your call to dequeueReusableCell(withIdentifier:) is failing, which is usually caused by having no prototype cells with the identifier you requested. First: check that you have a prototype cell registered.

What is DequeueReusableCell?

DequeueReusableCell(String) Returns a reusable table view cell that was created with the given ReuseIdentifier. DequeueReusableCell(NSString, NSIndexPath) Returns a reusable table view cell for the given reuseIdentifier , properly sized for the indexPath .

How do you link a prototype cell to a storyboard?

From Storyboard: In Attribute Inspector set Reuse Identifier. Add your CustomCell class in your storyboard like mentioned by Jignesh Agola.


1 Answers

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.

like image 77
cscott530 Avatar answered Sep 27 '22 19:09

cscott530