尝试通过UITableViewCell的滑动手势更新FMDB

时间:2013-11-18 22:01:16

标签: ios sqlite fmdb

有没有办法通过UITableViewCell的滑动手势更新FMDB?我希望能够使用现有的删除按钮来更改表中的单个列,但我尝试的所有内容似乎都会导致应用程序崩溃。我还希望能够创建一个插入按钮来执行类似的操作但无法在不创建子视图的情况下找到执行此操作的代码。我发布了一个我尝试过的事情的例子。

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

}

1 个答案:

答案 0 :(得分:1)

试试这个:

//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
    }
}