Reputation: 21
I am getting that error with this piece of code. Can you help me figure out why.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Restaurant *restaurant = [self.restaurants objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[NSString stringWithFormat:@"%@ %@",restaurant.Name,restaurant.Address]];
return cell;
}
Upvotes: 2
Views: 1131
Reputation: 726589
The problem with your code is that dequeueReusableCellWithIdentifier:
does not always return you a valid cell; sometimes, it returns nil
. In such cases you need to allocate a new instance of your cell, and then initialize it the usual way. As it currently stands, your method would return nil
if one is returned from the dequeueReusableCellWithIdentifier:
method.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyle1 reuseIdentifier:CellIdentifier];
}
Upvotes: 3