Mohan Kumar
Mohan Kumar

Reputation: 334

app crash due to UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: in iOS 5.1 Simulator

Its works fine on iOS 6 simulator. On iOS 5.1 Simulator when i run it for the very first time it get crashed due to the following exception.

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Here's my code

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    cell = [theTableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            [[NSBundle mainBundle] loadNibNamed:@"listTableCell" owner:self options:NULL];
        }
        else{
            [[NSBundle mainBundle] loadNibNamed:@"listTableCell_iPad" owner:self         options:NULL];
        }
        cell = nibLoadCellForListDetails;
    }
    return cell;
}

And in listTableCell.xib I set the File's Owner as my table view controller. And i made an outlet as nibLoadCellForListDetails in my table view controller correctly.

Upvotes: 0

Views: 899

Answers (2)

Anil Varghese
Anil Varghese

Reputation: 42977

Give a try like this

if (cell == nil) {
    NSArray *nib;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

      nib =   [[NSBundle mainBundle] loadNibNamed:@"listTableCell" owner:self options:NULL];
    }
    else{
      nib =  [[NSBundle mainBundle] loadNibNamed:@"listTableCell_iPad" owner:self         options:NULL];
    }
    cell = [nib objectAtIndex:0];
}

Upvotes: 0

Cocoadelica
Cocoadelica

Reputation: 3026

Looks like no cell has actually been created. Add in:

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

instead of:

cell = [theTableView dequeueReusableCellWithIdentifier:cellIdentifier];

Upvotes: 1

Related Questions