当我向表格视图添加一行时,为什么我的应用程序崩溃? [英] Why does my app crash when I add an row to the table view?

查看:20
本文介绍了当我向表格视图添加一行时,为什么我的应用程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按下按钮时,我正在添加这样的一行.它必须是第一部分的第一行:

I'm adding a row like this, when a button is pressed. It must be the very first row in the first section:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

我不会在这里调用任何其他东西.没有 -reloadData.我一直收到这个:

I don't call anything else here. No -reloadData. I keep getting this:

*** 由于未捕获的异常而终止应用程序'NSInternalInconsistencyException',原因:'无效更新:无效第 0 节中的行数.包含在一个中的行数更新后的现有部分 (6)必须等于行数载于该条之前更新(6),加减号从中插入或删除的行数部分(插入 1 个,删除 0 个).'

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).'

推荐答案

正如错误所说,您必须在数据源中插入一行,并告诉 tableView 为新行设置动画.在您的代码(您尚未发布)中,您可能需要调用 numberOfRowsInSection.在此 insertRowsAtIndexPaths 调用时,它应该返回 7(因为您正在添加 1)但它返回 6.

As the error says, you must insert a row into your data source as well as telling the tableView to animate the new row. In your code (that you haven't posted) you probably have a call to numberOfRowsInSection. At the time of this insertRowsAtIndexPaths call, it should be returning 7 (because you are adding one) but instead it is returning 6.

这篇关于当我向表格视图添加一行时,为什么我的应用程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆