使用场景:在使用UITableView
时,我为了达到可以侧滑点击删除的效果,调用了deleteRowsAtIndexPaths
方法,结果导致程序崩溃。
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[_bookModel.markArr removeObject:markModel];
}
错误提示:
*** 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 (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
原因:0.0 自己英语不好,但根据错误提示可以看到崩溃的原因和section
有关。
后来在网上查资料,找到了崩溃的原因:
- 在section
中,删除了最后一个row
的时候,而section
依然存在,并没有删除。
解决办法:不调用deleteRowsAtIndexPaths
方法,调用reload
方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[_bookModel.markArr removeObject:markModel];
[_tabView reloadData];
}