I have created a custom UITableViewCell
, but when I dequeue the cell, sometimes it throws an NSInvalidArgumentException
:
[UITableViewCell nameLabel]: unrecognized selector sent to instance 0x3b4e7f0
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[UITableViewCell nameLabel]: unrecognized selector sent to instance 0x3b4e7f0'
Now, my custom UITableViewCell
does have an attribute nameLabel
, so I am confused why it is throwing this error. Below is the code I use to dequeue the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
CTMenuItemVO* key = [[[self retrieveCartItems] allKeys] objectAtIndex:row];
NSNumber* quantity = [[self retrieveCartItems] objectForKey:key];
static NSString* SectionsTableIdentifier = @"SectionsTableIdentifier2";
OrderItemCell* cell = (OrderItemCell*)[tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"OrderItemCell"
owner:nil
options:nil];
for(id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (OrderItemCell*) currentObject;
break;
}
}
}
cell.nameLabel.text = key.Name;
cell.qtyLabel.text = [quantity stringValue];
return cell;
}
UPDATE
Changing the isKindOfClass:[UITableViewCell class]
check to an OrderItemCell
produces another error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
I guess this occurs because it leaves the for
loop without assigning the class.