具有混合静态/动态内容的 UITableViewController 中的 NSRangeException

Posted

技术标签:

【中文标题】具有混合静态/动态内容的 UITableViewController 中的 NSRangeException【英文标题】:NSRangeException in a UITableViewController with mixed static/dynamic content 【发布时间】:2013-04-17 14:59:51 【问题描述】:

使用 ios6 和故事板,我有一个包含 3 个部分的 UITableViewController。

前两个部分是静态的,第三个是动态的。

我已覆盖 numberOfRowsInSectioncellForRowAtIndexPathheightForRowAtIndexPath 以正确地推迟到第 0 节和第 1 节的 super,并从我的数据模型中为第 2 节返回适当的值。

当我的数据中的行数超过故事板中为第三部分配置的行数时,我仍然在 main() 中收到 NSRangeException:

'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** First throw call stack: (0x22f3012 0x178ae7e 0x22a8b44 0xb0ece4 0x8feee3 0x784f13 0x71940c 0x784a7b 0x789919 0x7899cf 0x7721bb 0x782b4b 0x71f2dd 0x179e6b0 0x558fc0 0x54d33c 0x54d150 0x4cb0bc 0x4cc227 0x4cc8e2 0x22bbafe 0x22bba3d 0x22997c2 0x2298f44 0x2298e1b 0x26867e3 0x2686668 0x6ceffc 0x2c4d 0x2b75) libc++abi.dylib: terminate called throwing an exception

为了防止发生此异常,我必须重写哪些超类方法?

【问题讨论】:

【参考方案1】:

我缺少的实现是:

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath return 0;

这意味着总共有四个方法必须重写才能将动态行为注入静态原型 UITableViewController:

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

希望这可以节省一些时间。

【讨论】:

天哪,你是对的。我永远不会猜到。过去 24 小时有同样的问题,我的头发被扯掉了。谢谢!!!!!!!!!!

以上是关于具有混合静态/动态内容的 UITableViewController 中的 NSRangeException的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法在 Laravel 4 中混合静态路由和动态路由?

Terraform:如何创建具有动态和静态内容的块

内容应用程序对话框中具有动态值的静态字段

如何使具有动态或静态内容的 div 居中?

如何在具有分离服务器的环境中使用 GWT RPC:静态内容服务器和动态内容服务器

如何将 Gatsby 混合站点连接到 Apollo/Graphcool(或 Redux)?