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

旧街凉风 提交于 2019-12-13 03:01:45

问题


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. 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).'


回答1:


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.



来源:https://stackoverflow.com/questions/2659742/why-does-my-app-crash-when-i-add-an-row-to-the-table-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!