简体   繁体   English

在表视图中添加一行时,为什么我的应用程序崩溃?

[英]Why does my app crash when I add an row to the table view?

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];

I don't call anything else here. 我在这里什么都没叫。 No -reloadData. 没有-reloadData。 I keep getting this: 我不断得到这个:

*** 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).' ***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效的更新:第0节中的行数无效。更新(6)后现有节中包含的行数必须等于行数该部分包含在更新(6)之前,加上或减去从该部分插入或删除的行数(插入1,删除0)。

As the error says, you must insert a row into your data source as well as telling the tableView to animate the new row. 如错误所述,您必须在数据源中插入一行,并告诉tableView为新行设置动画。 In your code (that you haven't posted) you probably have a call to numberOfRowsInSection . 在您的代码中(尚未发布),您可能会调用numberOfRowsInSection At the time of this insertRowsAtIndexPaths call, it should be returning 7 (because you are adding one) but instead it is returning 6. 在此insertRowsAtIndexPaths调用时,它应该返回7(因为您要添加一个),但是返回6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM