Question

Is there a way to update an FMDB via swipe gesture of a UITableViewCell? I would like to be able to use the existing delete button to change a single column in the table but everything I try seems to result in the application crashing. I would also like to be able to create an insert button to do something similar but cannot find the code to do this without creating a subview. I posted an example of one of the things I have tried.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source

    [tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationFade];

    NSString *docsDir;
    NSArray *dirPaths;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // Get the documents directory

    docsDir = dirPaths[0];

    NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"passages.db"]];

    self.database = [FMDatabase databaseWithPath:path];
    [self.database open];

    [self.database executeUpdate:@"UPDATE passages set favorite=?", @NO, nil];
    [self.database close];

}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
[self.tableView reloadData];

}

Was it helpful?

Solution

Try this:

//Doing this should give you the swipe to delete functionality out of the box
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
      return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
     if (editingStyle == UITableViewCellEditingStyleDelete) {
       //your code for updating favorite
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top