ios 解决bug---UITableView删除到最后一个unable to generate a new section map with old section count: 1 and new

来源:互联网 发布:mac上好玩的单机游戏 编辑:程序博客网 时间:2024/05/18 19:39
在iOS的UItableview删除中,删除操作我们经常用这样的语句
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {        if (editingStyle == UITableViewCellEditingStyleDelete) {              [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationFade];            }      else if (editingStyle == UITableViewCellEditingStyleInsert) {        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.    }    }


但是每次删除到最后一行时就会报错,报错提示是这样的:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView internal bug: unable to generate a new section map with old section count: 1 and new section count: 0'




这是因为 tableView 是分组的,删除最后一个分组中最后一个cell 和数据源时 也要对 session 进行操作

正确的处理方法为:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    [tableView beginUpdates];    if (editingStyle == UITableViewCellEditingStyleDelete) {                            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];        }       [tableView reloadData];        else if (editingStyle == UITableViewCellEditingStyleInsert) {           }    [tableView endUpdates];}


在这里我加入了 
 [tableView beginUpdates];
 [tableView endUpdates];
并且还需要对最后一个section做删除操作

 [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];



0 0
原创粉丝点击