关于删除cell某行和清除sqlite对应数据问题

在iOS开发中,删除TableView某行时需先从SQLite数据库中移除对应数据,以避免NSInternalInconsistencyException异常。错误提示表明更新后Section 0的行数不匹配。此外,如果先删除行再删除数据,可能导致因已删除的行导致的索引越界问题。

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

删除某一tableview的某一行并且删除对应数据。


 先删数据库  再删table view!

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSLog(@"%s",__func__);
    
    if(editingStyle == UITableViewCellEditingStyleDelete){

        //同步数据
        makeupModel *makeup = self.makeup[indexPath.row];
        [makeup deleteObject];
        //删除联系人
        
        
        
         [self.makeup removeObjectAtIndex:indexPath.row];
        //刷新表格
        NSIndexPath *deleteIndex = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
        
        [self.tableView deleteRowsAtIndexPaths:@[deleteIndex]
                              withRowAnimation:UITableViewRowAnimationFade];

        
    }
    
    
    
}

否则会报错:


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


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



我还遇到了一个越界的报错,估计也和代码顺序有关系 如果先删除这一行 在删除数据的时候,因为这行已经删除了,对应的indexpath.row找不到了就越界。。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值