---------------------------------------------错误写法-------------------------------------------------------
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
command+R运行上面程序,然后选中某一行从右往左拖(如下图)会出现以下错误:
*** 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 (97) must be equal to the number of rows contained in that section before the update (97), 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).'
这个错误的大致意思是说当调用这个方法后,也就是更新前后数组的长度必须保持一致。具体如下
-----------------------------------------正确写法-----------------------------------------------
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
// 从模型数组中取出的模型,(这个时候模型属性必须要是可变数组)
DHModel *model = self.models[indexPath.row];
// 实现这个方法之前,先要确定下面要删除多少行,下面删除多少就要对应对模型数组中的removeObject:方法
[self.models removeObject:model];
[tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}