iphone - UITableView cellForRowAtIndexPath 问题

标签 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;

当我尝试加载 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:'

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

最佳答案

在我看来,您有一个部分可见的 switch case,它正在返回 case 1 的单元格。 您的错误让我认为您在 switch case 中的某个分支返回了 null UITableViewCell

确保该 switch case 之外的所有路径都返回有效的 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;
}

关于iphone - UITableView cellForRowAtIndexPath 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1972625/

相关文章:

iphone - 为什么 Youtube 视频没有填满 UiWebView?

ios - 如何从主 ViewController 访问自定义 UITableViewCell

swift - 如何使用 indexpathforselectedrow 从结构数组中获取实例?

iphone - 不同的 UITableViewCell 高度

iphone - 从 UITableViewCell 转到另一个 ViewController

iphone - 如何在不进行任何解析的情况下从服务器检索数据?

ios - 添加自定义按钮后,将后退按钮添加回 navigationController

iphone - iOS UITableView 错误

iphone - 如何在 iPhone 应用程序中设置硬件音量?

ios - 动态显示来自服务 Objective-C 的状态消息