uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of ro

本文探讨了在iOS开发过程中遇到的删除数据时报错的问题,并提供了有效的解决方法。通过先删除数据库中的数据,再刷新UITableView,避免了因数据更新导致的系统错误。同时解释了错误产生的原因及解决方案,帮助开发者避免类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、问题

删除一行时,报错。。。。

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 (0) must be equal to the number of rows contained in that section before the update (3), 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).'



2、解决办法

背景:

- (void)loadData {

   

        NSMutableArray* mutArray= //[DAO getdata]; 从数据库或是数据源取数据

        Array* array = [mutArray mutableCopy];  //因为要动态添加的数据,所以用了NSMutableArray,  array和mutArray 装得数据其实是一样的

        [self.tableView reloadData];



办法: 先删数据库  再删table view。。

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

                 

                

        //[DAO delete]  先数据库删除,实现略

        [mutArray removeObjectAtIndex:indexPath.row];

         array = [mutArray mutableCopy];

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


3、原因



deleteRowsAtIndexPaths后Section里面的rows数量,和系统调用numberOfRowsInSection时rows数量不一致


可以Bug 去看 。。。。



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.

     NSLog(@"numberOfRowsInSection==%lu",(unsigned long)[array count]);

     return [array count];

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值