objective-cuitableviewios5.1

iOS 5.1 dequeueReusableCellWithIdentifier throws NSInvalidArgument exception


I have an app that has been working great on iOS6.1. Today I realized I should probably try to make this compatible for iOS5 as well. I tried running it on the iOS 5 simulator and am getting an exception thrown on my dequeCell method call. I can't figure out why as it works wonderfully on iOS6. Anyone else come across this problem?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = //throws exception here
     [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                     forIndexPath:indexPath];
    cell.accessoryView = nil;
    cell.backgroundColor = [UIColor colorWithRed:.97 
                                           green:.97 
                                            blue:.97 
                                           alpha:1];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    ...
    return cell;
}

-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized 
 selector sent to instance 0x8a3a000 -*** Terminating app due to uncaught 
 exception 'NSInvalidArgumentException', reason: '-[UITableView 
 dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector 
 sent to instance 0x8a3a000'

Solution

  • To my knowledge, the method dequeueReusableCellWithIdentifier:forIndexPath: was added in iOS 6.0 only.

    Better use dequeueReusableCellWithIdentifier: