我正在尝试用多个部分填充 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/