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

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];

}




你可能感兴趣的:(ios,update,Invalid)