执行TableView的deleteRowsAtIndexPaths报Invalid update错误

       在网上搜索了一大堆,发现都说得没头没尾的。其实最主要的原因是在执行deleteRowsAtIndexPaths方法前要将列表数据源中对应的位置的数据删除掉,不然在执行这个方法后就会报错。比如原来列表中有10条数据的,现在想删除一条数据,可是在没有将该条数据从数据源中删除或者没有删除成功时,就去执行deleteRowsAtIndexPaths方法,UITableViewController不会去检查你是否删除成功,它就认为现在是9条数据,但是当它检查当前数据数量时发现现在数量还是10条,与要求不符合,所以就报如下错误:

    

2018-04-11 23:42:12.412066+0800MyNotesObjc[3050:108200] *** Assertion failure in -[UITableView_endCellAnimationsWithContext:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UITableView.m:2011

2018-04-11 23:42:12.419054+0800MyNotesObjc[3050:108200] *** Terminating app due to uncaught exception'NSInternalInconsistencyException', reason: 'Invalid update: invalid number ofrows in section 0.  The number of rows contained in an existing sectionafter the update (2) must be equal to the number of rows contained in thatsection before the update (2), plus or minus the number of rows inserted ordeleted from that section (0 inserted, 1 deleted) and plus or minus the numberof rows moved into or out of that section (0 moved in, 0 moved out).'


下面是我对这个问题做的一个记录:

1)错误信息:

2018-04-11 23:42:12.412066+0800MyNotesObjc[3050:108200] *** Assertion failure in -[UITableView_endCellAnimationsWithContext:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UITableView.m:2011

2018-04-11 23:42:12.419054+0800MyNotesObjc[3050:108200] *** Terminating app due to uncaught exception'NSInternalInconsistencyException', reason: 'Invalid update: invalid number ofrows in section 0.  The number of rows contained in an existing sectionafter the update (2) must be equal to the number of rows contained in thatsection before the update (2), plus or minus the number of rows inserted ordeleted from that section (0 inserted, 1 deleted) and plus or minus the numberof rows moved into or out of that section (0 moved in, 0 moved out).'

 

2错误代码

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        Note *note = self.listData[indexPath.row];

       //获得DAO对象

        NoteDAO *dao = [NoteDAOsharedInstance];

       //删除数据

        [dao remove:note];

       //重新查询所有数据

        self.listData = [dao findAll];

        

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }

}

 

3)错误原因:

          由于在执行删除数据时,执行失败了,没有删除成功。导致重新查询所有数据后数据还是原来的数量,没有减少。而程序在更新数据时,发现数据并没有减少,与要求的数量不对,从而报错。

 

4解决办法

         检查NoteDAO类中的的remove方法,发现是由于在对比时间时失败了,导致没有删除成功,解决了这个问题后报错也解决了。

// 删除Note方法

- (int)remove:(Note *)model {

    NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:self.plistFilePath];

    for (NSDictionary *dict in array) {

        NSString *strDate = dict[@"date"];

        NSDate *date = [self.dateFormater dateFromString:strDate];

        

       //比较日期主键是否相等

        if ([date isEqualToDate:model.date]) {

           [array removeObject:dict];

           [array writeToFile:self.plistFilePath atomically:TRUE];

            break;

        }

    }

    return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值