iphone - 从表和 sqlite 数据库中删除行

标签 iphone objective-c database sqlite

我在从数据库和表中删除行时遇到了一些问题。

问题是:我可以滑动行,点击“删除”按钮,但没有任何反应。

可能我做错了。

有人可以给我提示吗? 如果您想看一下,这里有所有程序:(更新)

http://cl.ly/9q7U

-(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];
    [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //Here i got the SIGABRT error
    [_tableView endUpdates];

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

}
}

#

感谢 Akshay,我终于修复了这部分代码。我在这里为需要它的人写下解决方案。

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

    [tableView reloadData];
}
}

最佳答案

你应该调用:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
        withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

commitEditingStyle: 中,而不是 reloadData。此外,确保从数据库中删除实体并从 numberOfRowsInSection: 中少返回一个实体。

关于iphone - 从表和 sqlite 数据库中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7298502/

相关文章:

iphone - 如何将 NSDictionary 中的 Key 浮点值添加到 NSarray?

iphone - AudioServicesAddSystemSoundCompletion 回调方法在几次调用后没有被调用

ios - UIImageView 图片名称比较问题

objective-c - 使用 Storyboard在 iPAD 上支持多个方向的最佳方法(无 AutoLayout 或 AutoResizing)

ios - iPhone 应用内购买商店工具包错误-1003 "Cannot connect to iTunes Store"

iphone - UIManagedDocument saveToURL 始终返回 false

ios - 在方法上计算给定数字会出现此错误

android - 房间 : error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: abc)

sql - 比较行并返回与所有记录匹配的列(POSTGRESQL)

java - 查询没有返回结果