从表和sqlite数据库中删除行

时间:2011-09-05 09:42:18

标签: iphone objective-c xcode sqlite tableview

我仍然需要你的帮助 我有这段代码不想工作。

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

if (editingStyle == UITableViewCellEditingStyleDelete) {

    NSDictionary *rowVals = (NSDictionary *) [shoppingListItems objectAtIndex:indexPath.row];
    NSString *keyValue = (NSString *) [rowVals objectForKey:@"key"];

    [tableView beginUpdates];

    sqlite3 *db;
    int dbrc; //Codice di ritorno del database (database return code)
    DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate*) [UIApplication sharedApplication].delegate;
    const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
    dbrc = sqlite3_open(dbFilePathUTF8, &db);
    if (dbrc) {
        NSLog(@"Impossibile aprire il Database!");
        return;
    }

    sqlite3_stmt *dbps; //Istruzione di preparazione del database

    NSString *deleteStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", keyValue];
    const char *deleteStatement = [deleteStatementsNS UTF8String];
    dbrc = sqlite3_prepare_v2(db, deleteStatement, -1, &dbps, NULL);
    dbrc = sqlite3_step(dbps);


    //faccio pulizia rilasciando i database
    sqlite3_finalize(dbps);
    sqlite3_close(db);

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [shoppingListItems removeObjectAtIndex:indexPath.row];
    [tableView reloadData];

    [tableView endUpdates];
}
}

实际上,它工作正常。我可以在表格中滑动到一行,删除它,我有淡入淡出效果,内容将从数据库和表格中删除。
但是,如果我尝试在第一个元素之后删除另一个元素,那么我就有了一个SIGABRT

    [shoppingListItems removeObjectAtIndex:indexPath.row];

如果我移动到另一个标签,请返回并删除一行,一切正常...... 有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我希望shoppingListItems数组包含字典。所以,对象在数组中你可以直接从数组中删除该特定对象。 试试这个......

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

if (editingStyle == UITableViewCellEditingStyleDelete) {

    NSDictionary *rowVals = (NSDictionary *) [shoppingListItems objectAtIndex:indexPath.row];
    NSString *keyValue = (NSString *) [rowVals objectForKey:@"key"];
    sqlite3 *db;
    int dbrc; //Codice di ritorno del database (database return code)
    DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate*) [UIApplication sharedApplication].delegate;
    const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
    dbrc = sqlite3_open(dbFilePathUTF8, &db);
    if (dbrc) {
        NSLog(@"Impossibile aprire il Database!");
        return;
    }

    sqlite3_stmt *dbps; //Istruzione di preparazione del database

    NSString *deleteStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", keyValue];
    const char *deleteStatement = [deleteStatementsNS UTF8String];
    dbrc = sqlite3_prepare_v2(db, deleteStatement, -1, &dbps, NULL);
    dbrc = sqlite3_step(dbps);


    //faccio pulizia rilasciando i database
    sqlite3_finalize(dbps);
    sqlite3_close(db);
    [shoppingListItems removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [tableView reloadData];
}
}

答案 1 :(得分:1)

您可能想要交换

[tableView reloadData];
[tableView endUpdates];

使其成为

[tableView endUpdates];
[tableView reloadData];

或者,请告诉我们SIGABRT的错误消息是什么。