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 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] *由于未捕获的异常而终止应用程序'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;

}

应用程序不是每次都崩溃有时有时工作有时崩溃

4

3 回答 3

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。该 ID 是您的数据库所必需的。

于 2013-04-08T12:03:23.443 回答
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];周围的代码行。

于 2013-04-08T11:45:08.010 回答
0

IT"S WORKING at and of your commitEditingStyle 方法添加这一行

[TableViewForFirstView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
于 2015-06-01T13:16:58.923 回答