UITableView有关cellForRowAtIndexPath的问题

Hay*_*ene 18 iphone cocoa-touch uitableview

我试图用多个部分填充uitableview.这是我的代码:

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;
Run Code Online (Sandbox Code Playgroud)

我尝试加载视图时收到此错误:

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:'
Run Code Online (Sandbox Code Playgroud)

该索引处的数组记录为合法字符串,但不会正确返回单元格.因此它不会超过这一点.请帮忙.

Bro*_*olf 28

在我看来,你有一个部分可见的开关案例,它返回一个案例1的单元格.你的错误让我觉得你UITableViewCell在你的开关案例中返回null 某个分支.

确保该开关盒外的所有路径都返回一个有效的UITableViewCell对象.

案例0你还有什么回报?换句话说,你为UITableView请求的第一行返回了什么?

以下是我的一个项目的代码示例,它可能为您提供一个良好的起点,以查看您出错的地方:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

  • +1如你所说,几乎可以肯定代码缺少默认值:case或者在某些代码路径上返回nil. (2认同)

Ben*_*ieb 2

不知道为什么你有“案例 1:”独自坐在那里,但如果它是案例 1 以外的情况,那么这将失败,并且此后发生的任何事情都将被返回。确保您始终返回单元格。