简体   繁体   中英

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.

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];

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM