2011-09-04 154 views
1

我有一些問題,從數據庫和表中刪除行。從表和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); 

} 
} 

感謝阿克沙伊,最後我固定的代碼的這一部分。我在這裏給那些需要它的人寫解決方案。

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

@Oiproks在提問時,請始終告訴我們您是否有某種錯誤,或者您有意外的行爲,以及期望的行爲是什麼(這種情況在這種情況下很明顯,但可能並不明顯一般情況下)。 –

+0

好的,對不起!我解決了這個問題。 – Oiproks

+0

@Oiproks對不起,我的代碼在NSString * keyValue =(NSString *)[rowVals objectForKey:@「key」]處崩潰了;請幫助我 –

回答

2

你應該打電話:

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

裏面的commitEditingStyle:而不是reloadData。另外,請確保從DB &中刪除實體,從numberOfRowsInSection:返回一個較少的實體。

+0

我按照你的說法試過,但是我在「[tableView endUpdates];」上收到SIGABRT,並且我不知道如何返回少一行... :(我不那麼熟練...... – Oiproks

+0

你在numberOfRowsInSection返回什麼:?如果你在刷卡前有3行並返回3,現在返回2. – Akshay

+0

那裏我返回從數據庫收取數組shoppingListItems(返回[shoppingListItems計數];)的項目數。我怎麼能告訴它數少一個? – Oiproks

1

我認爲這個問題是在這條線

NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key=?", indexPath]; 

我想你想在insertStatementsNS此:

NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key=%@", indexPath]; 

NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", indexPath]; 
+0

呃..問題不僅在於!其實我不能看到,如果這是一個工作與否因爲我刷卡並刪除沒有任何反應......可能有其他地方有什麼錯:S – Oiproks

+0

commitEditingStyle調用? –

+0

放置斷點並檢查是否調用了'commitEditingStyle:' – Nekto