Delete row from table and sqlite database

前端 未结 2 1618
感动是毒
感动是毒 2021-01-25 08:25

I still need your help.
I have this piece of code that doesn\'t want to work.

-(void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellE         


        
相关标签:
2条回答
  • 2021-01-25 08:41

    You might want to swap around

    [tableView reloadData];
    [tableView endUpdates];
    

    to make it

    [tableView endUpdates];
    [tableView reloadData];
    

    Alternatively please tell us what the error message for the SIGABRT is.

    0 讨论(0)
  • 2021-01-25 09:01

    i hope shoppingListItems array contains Dictinaries . so have that Object In Array u can Directly Delete that particular object From Array. TRY LIKE THIS.....

      -(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 讨论(0)
提交回复
热议问题