1. 程式人生 > >Invalid update: invalid number of rows in section 0. The number of rows contained in an ....

Invalid update: invalid number of rows in section 0. The number of rows contained in an ....

報錯提示:

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

我是在使用tabelView刪除一行cell時 報這個錯.
objectC
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSMutableArray *tempModelAry = [NSMutableArray arrayWithArray:self.modelAry];
[tempModelAry removeObjectAtIndex:indexPath.row];
self.modelAry = tempModelAry.copy;
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];

原因是 self.modelAry = tempModelAry.copy; 我在self.modelAry的set方法裡寫了 tabelView reload 的方法.

解決辦法是 把self.modelAry 變成 _modelAry 就好了.