2011-09-05 5 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

나도 몰라 ..... 이런 식으로 TRY ... 그리고 마지막 행에 문제도있다 ... – Oiproks

답변

2

나는 shoppingListItems 배열에 Dictinaries가 포함되기를 바랍니다. 그래서 그 배열에서 개체를 배열에서 삭제할 수 있습니다. 하지 않는 때로는, 때로는이 코드가 작동 ... 내 Mac 뭐가 잘못

-(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

네, 거기에 사전이 있습니다. 나는 네가 쓴대로했다. 나는 노력했고 그것은 ... 한 번 (LOL) 일했다. 이제 나는이 "EXC_BAD_ACCESS"같은 행에 있습니다. – Oiproks

+0

왜 [tableView beginUpdates];'&'[tableView endUpdates];'없이 작업하고 있습니까? 어쨌든 작동합니까? 나는 다른 차이점을 볼 수 없다 ... – Oiproks

+0

인수에 NSZombieanbled 예 ..... 충돌 보고서보기 – Srinivas

1

은 약

[tableView reloadData]; 
[tableView endUpdates]; 

[tableView endUpdates]; 
[tableView reloadData]; 

대안 SIGABRT에 대한 오류 메시지가 무엇인지 알려주세요 확인합니다.

+0

좋아,이 문제를 해결하기 위해 뭔가를하려고 할 때 어쩌면 내가 잘못 썼을 수도 있지만 그게 문제가 아니 었습니다. 내가 Srinivas에 지금 쓴 것처럼 나는 또 다른 문제가있다 ... – Oiproks

관련 문제