Peter Stuart
Peter Stuart

Reputation: 2434

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] error

I get this error when attempting to populate my table:

* Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]

Here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary *dictionary = tasks[indexPath.row];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = dictionary[@"key"];
    cell.detailTextLabel.text = [Global getPriority:(NSString *)dictionary[@"key_2"]];

    return cell;
}

When I remove the forIndexPath the error goes away but then I can use the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method.

Could somebody please help me?

Also, the table is built programmatically.

Thanks,

Peter

Upvotes: 0

Views: 4281

Answers (2)

Zeeshan
Zeeshan

Reputation: 4244

I faced same issue in my code. what i found was I had accidentally attached delegate of UITableView to view of View controller. Change delegate to UIViewControllerenter image description here

Upvotes: 1

dariaa
dariaa

Reputation: 6385

dequeueReusableCellWithIdentifier:forIndexPath: expects that you have previously registered a nib or a class for the same reuseIdentifier (or that you have a prototype cell in your nib/storyboard again with the same reuseIdentifier. It also is guaranteed that this method will alwaysreturn a valid cell, so you would never have to creat a cell programmatically if your are using dequeueReusableCellWithIdentifier:forIndexPath: method.

If you do want to create your cells programmatically, then you should use dequeueReusableCellWithIdentifier paired with your if (!cell) { cell =... } code.
For more information check out this answer

Upvotes: 1

Related Questions