从表视图中删除单元格时崩溃

时间:2013-04-08 11:33:04

标签: ios tableview

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(editingStyle == UITableViewCellEditingStyleDelete) {

        //Get the object to delete from the array.
        NSLog(@"%@",[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] );

        NSInteger myInteger = [[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] integerValue];
      NSNumber *selRow = [NSNumber numberWithInt:myInteger];
        NSLog(@"%@",selRow);
        [self removeCell:selRow]; // this is working perfectly it gets remove from database

        //Delete the object from the table.
        // app get crash here crash report is pested below 
        [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }
}

- (void) removeCell:(NSNumber *)selRow{

       NSLog(@"_arrayForTable%@",_arrayForTable);
 NSInteger myInteger = [selRow integerValue];

    [_arrayForTable removeObjectAtIndex:myInteger];

    NSLog(@"_arrayForTable%@",_arrayForTable);



    if(deleteStmt == nil) {
        const char *sql = "delete from PersonNamesAndBirthDates where ID = ?";
        NSLog(@"%s",sql);
        if(sqlite3_prepare_v2(database1, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database1));
    }
    NSLog(@"%d",myInteger);

    //When binding parameters, index starts from 1 and not zero.
    sqlite3_bind_int(deleteStmt, 1, myInteger );

    if (SQLITE_DONE != sqlite3_step(deleteStmt)) 
        NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database1));

    sqlite3_reset(deleteStmt);
}
  

*断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-1912.3 / UITableView.m:1046 2013-04-08   17:01:13.069生日提醒2 [583:15803] * 终止app到期   未捕获的异常'NSInternalInconsistencyException',原因:   '无效更新:第0节中的行数无效。数量   更新(12)后必须包含在现有部分中的行   等于之前该部分中包含的行数   更新(12),加上或减去插入或删除的行数   该部分(0插入,1删除)和加号或减号的数量   移入或移出该部分的行(0移入,0移出)。'

请提出建议

我的numberofrow方法如下

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

    // Return the number of rows in the section.


    return _arrayForTable.count;

}

应用程序不会在每次某些时候崩溃时崩溃

3 个答案:

答案 0 :(得分:1)

好的,正如我所看到的那样,您将获得特定行的ID:

NSInteger myInteger = 
    [[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] integerValue];
NSNumber *selRow = [NSNumber numberWithInt:myInteger];

然后,您正在使用该特定行的ID作为从数组中删除它的索引。

NSInteger myInteger = [selRow integerValue];
[_arrayForTable removeObjectAtIndex:myInteger];

那就是出错的地方。如果数组与TableView具有相同的顺序,则需要从中删除indexPath.row。该数据库是您的数据库所必需的。

答案 1 :(得分:0)

试试这个:

- (void)tableView:(UITableView *)tv commitEditingStyle :(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(editingStyle == UITableViewCellEditingStyleDelete) {

        //Get the object to delete from the array.
        NSLog(@"%@",[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] );

        NSInteger myInteger = [[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] integerValue];
        NSNumber *selRow = [NSNumber numberWithInt:myInteger];
        NSLog(@"%@",selRow);

        [self.table beginUpdates];        

        [self removeCell:selRow]; // this is working perfectly it gets remove from database

        //Delete the object from the table.
        // app get crash here crash report is pested below 
        [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [self.table endUpdates];

    }
}

请注意改变tableview的代码行周围的[self.table beginUpdates];[self.table endUpdates];

答案 2 :(得分:0)

IT“工作 at和commitEditingStyle方法添加此行

async