views:

40

answers:

3

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.

+1  A: 

Are all the objects in topLevelObjects of type OrderItemCell? There is a possibility that you are assigning a different object to cell, typecasting it to a different object and therefore the unrecognized selector error.

hyn
Sorry, no. Instead of throwing the previously stated Exception, I get another: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:, assuming that it leaves the 'for' loop without finding any cells of that particular kind.
Wayne Hartman
A: 

Change [UITableViewCell class] to [OrderItemCell class].

tc.
+1  A: 

Turns out there were two different copies named OrderItemCell. Not sure how the doppelganger got into my folder structure (even though there was only one referenced in my workspace), but once I removed the impostor, it worked like a charm every time.

Wayne Hartman