1

I'm building a UITableView with some cells in it. However, it keeps throwing unable to dequeue a cell with identifier cell. I'm using the code below, which should work. What am I doing wrong?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier];

    cell.textLabel.text = [self.ninjas[indexPath.row] name];

    NSString *imageUrl = [NSString stringWithFormat: @"%@", [self.ninjas[indexPath.row] thumbnail]];

    [cell.imageView setImageWithURL:[NSURL URLWithString:imageUrl]
                   placeholderImage:[UIImage imageNamed:@"50-50.jpg"]];

    return cell;
}

I'm not using a storyboard for this app.

user4334509
  • 125
  • 2
  • 10

1 Answers1

-1

In order to use dequeueReusableCellWithIdentifier:forIndexPath: (as opposed to just dequeueReusableCellWithIdentifier:), one needs to first register that reuse ID with the table view and a reference to the cell class or the xib using one of these functions

edit: also, if you're creating prototype cells with Interface Builder, you'll need to specify a resue ID for those cells as well.

Community
  • 1
  • 1
oltman
  • 1,772
  • 1
  • 14
  • 24
  • Yeah, I'm sorry, I should've mentioned this before, but I'm not using a storyboard for this app. – user4334509 Dec 30 '14 at 16:09
  • Then simply registering your `UITableViewCell` for your reuse ID after creating the `UITableView` should solve you problem. – oltman Dec 30 '14 at 17:59