Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Problems with cellForRowAtIndexPath

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.

like image 798
azban Avatar asked Dec 29 '09 02:12

azban


People also ask

What is Uitableview in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

What is Indexpath in Tableview Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.


2 Answers

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;
}
like image 146
Brock Woolf Avatar answered Oct 15 '22 13:10

Brock Woolf


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.

like image 26
Ben Gottlieb Avatar answered Oct 15 '22 13:10

Ben Gottlieb