2011-09-05 123 views
1

我仍然需要你的帮助。
我有这段不想工作的代码。从表和sqlite数据库删除行

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

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

[shoppingListItems removeObjectAtIndex:indexPath.row]; 

如果我移动到另一个选项卡,回去删除行一个SIGABRT,一切工作正常...
任何想法?

+0

我不知道这有什么错我的Mac ...有时此代码的工作,有时不我,而与最后一排也是个问题... – Oiproks

回答

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]; 
} 
} 
+0

是的,我有在那里的字典。我按你写的那样做了。我试过了,它的工作......只有一次(哈哈)。现在我在同一行上有这个“EXC_BAD_ACCESS”。 – Oiproks

+0

为什么你没有'[tableView beginUpdates];'&'[tableView endUpdates];''?无论如何它工作吗?我看不到任何其他区别... – Oiproks

+0

把NSZombieanbled YES在参数.....看到崩溃报告 – Srinivas

1

你可能要交换周围

[tableView reloadData]; 
[tableView endUpdates]; 

,使其

[tableView endUpdates]; 
[tableView reloadData]; 

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

+0

好的,我解决了这个问题,我写错了,因为某种原因,也许我正在尝试做某件事情......但那不是问题所在。正如我写信给斯里尼瓦斯现在我有另一个问题... – Oiproks