【发布时间】:2012-10-03 05:27:39
【问题描述】:
我正在尝试在分组表格视图中混合动态和静态单元格:我想在顶部获得 两个带有静态单元格的部分,然后是 动态单元格部分(请参考下面的截图)。我已将表格视图内容设置为静态单元格。
编辑
根据 AppleFreak 的建议,我将代码更改如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
if (indexPath.section <= 1) { // section <= 1 indicates static cells
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
} else { // section > 1 indicates dynamic cells
CellIdentifier = [NSString stringWithFormat:@"section%idynamic",indexPath.section];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
return cell;
}
但是,我的应用程序崩溃并显示错误消息
由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'UITableView 数据源 必须从 tableView 返回一个单元格:cellForRowAtIndexPath:'
对于第 0 节和第 0 行。从 cell = [super tableView:tableView cellForRowAtIndexPath:indexPath] 返回的第 0 节和第 0 行的单元格是 nil。
我的代码有什么问题?我的网点有什么问题吗?我没有设置任何插座,因为我正在继承UITableViewController 并且据说没有设置任何用于设置tableview 的插座(?)。有关如何更好地做到这一点的任何建议?
编辑二
我在故事板中重新创建了我的场景(请参阅上面更新的屏幕截图)并重写了视图控制器,以便从一个新的基础开始。我还按照 applefreak 的建议阅读了 Apple 论坛中的描述。但是,我使用numberOfSectionsInTableView:tableView 方法解决了我的第一个问题,其中我将静态部分的数量(两个)增加了一个。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [super numberOfSectionsInTableView:tableView] + 1 ; }
应用程序崩溃并显示错误消息:
由于未捕获的异常“NSRangeException”而终止应用程序,原因: '*** -[__NSArrayI objectAtIndex:]: 索引 2 超出范围 [0 .. 1]'
即使我遵循了 Apple 和 applefreak 的建议,为什么这段代码对我不起作用?在 iOS 6 中 tableView 可能发生了一些变化?
解决方案:我现在在下面的答案中使用 AppleFreaks 代码示例来解决这个问题。谢谢你,AppleFreak!
编辑 III:单元格选择:
如何在混合(动态和静态单元格)单元格表格视图中处理单元格选择?
我什么时候打电话给super,什么时候打电话给self tableView?
当我使用
[[super tableView] selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]
并尝试使用以下命令检查选定的索引路径:
UITableView *tableView = [super tableView];
if ( [[tableView indexPathForSelectedRow] isEqual:customGrowthIndexPath] ) { .. }
我得到nil 的返回值。
由于我找不到错误的根源,非常感谢您的帮助
【问题讨论】:
-
您是否按照课程文档注册了笔尖? '重要提示:在调用此方法之前,您必须使用 registerNib:forCellReuseIdentifier: 或 registerClass:forCellReuseIdentifier: 方法注册类或 nib 文件。'
标签: objective-c ios ios5 uitableview