當我們更改了dataSource(刪除了section或者row),并調用了[tableview reloadData],但- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section還沒來及的調用,卻調用了- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath導致數(shù)組越界崩潰。
正確的路子是刪除dataSource后對tableview的row及section進行刪除,而非reloadData。
[model.list removeObjectSafeAtIndex:indexPath.row];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
[self.dataArray removeObjectSafeAtIndex:index];
[self.tableView beginUpdates];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];