views:

2087

answers:

3

I am trying to fill a uitableview with multiple sections. This is my code:

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

case 1:
[[cell textLabel] setText:[usersTeachers objectAtIndex:(indexPath.row+1)]];
return cell;
break;

I get this error when I try to load the view:

2009-12-28 21:09:48.380 FSS[2046:207] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit/UIKit-984.38/UITableView.m:4709
2009-12-28 21:09:48.381 FSS[2046:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

The array at that index logs as a legitimate string but it does not return the cell properly. Thus it doesnt load past this point. Please help.

A: 

Not sure why you have "case 1:" sitting out there all alone, but if it's a case OTHER than 1, this is going to fall through, and whatever comes after this will be returned. Make sure you ALWAYS return a cell.

Ben Gottlieb
I understand. I have an entire switch statement set up. I just only used the first to give an example of what was happening. It doesnt run another case before it gives the error.
Arman Dufrene
A: 

You are probably not returning a cell for indexPath.row==0 (your 'case 1' is suspicious...). But can't say unless you post your entire switch statement.

Try returning 'cell' outside of your switch statement, you'll understand better what happens. You won't have an assertion failure then.

Zoran Simic
+5  A: 

It looks to me that you have a partially visible switch case where it is returning a cell for case 1. Your error makes me think you are returning a null UITableViewCell some branch in your switch case.

Make sure all paths out of that switch case return a valid UITableViewCell object.

What are you returning for case 0? In other words, what are you returning for the first row that the UITableView requests?

Here is a sample of code from one of my projects, It might give you a good starting point to see where you are going wrong:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSInteger section = [indexPath section];

    switch (section) {
        case 0: // First cell in section 1
            cell.textLabel.text = [collectionHelpTitles objectAtIndex:[indexPath row]];
            break;
        case 1: // Second cell in section 1
            cell.textLabel.text = [noteHelpTitles objectAtIndex:[indexPath row]];
            break;
        case 2: // Third cell in section 1
            cell.textLabel.text = [checklistHelpTitles objectAtIndex:[indexPath row]];
            break;
        case 3: // Fourth cell in section 1
            cell.textLabel.text = [photoHelpTitles objectAtIndex:[indexPath row]];
            break;
        default:
            // Do something else here if a cell other than 1,2,3 or 4 is requested
            break;
    }
    return cell;
}
Brock Woolf
+1 As you say, almost certainly the code is missing the default: case or is otherwise returning nil on some code path.
Rob Napier