2018-01-15

UItableView的删除操作

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除数据库数据
    DBUtil * db = [[DBUtil alloc] init]; 
    WordBook *item = [self.wordbookArray objectAtIndex:indexPath.row];
    [db deleteWordBookWithId:item._id withCallback:^(NSInteger code) 
{
        // 删除模型
        [self.wordbookArray removeObjectAtIndex:indexPath.row];
        // 刷新
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }];
} 

//修改Delete按钮文字为“删除”

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

这样是能是在tableview中删除一行,重新点击还会出现。

数据库中删除


-(void)deleteWordBookWithId:(unsigned int)bookid withCallback:(DBOperationResult)callback{
    //拿到FMDatabase对象
    FMDatabase *db = [DBUtil getDatabase];
    if([db open]) {
        NSString *checkTableExistSQL = @"CREATE TABLE IF NOT EXISTS wordbook (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL,createTime LONG NULL,wordNum INT NULL,finishNum INT NULL);";
        [db executeUpdate:checkTableExistSQL];
        //插入测试数据
        NSString *deleteSQL = [NSString stringWithFormat:@"delete from wordbook where id = %d;",bookid];
        [db executeUpdate:deleteSQL];
       
        callback(1);
        [db close];
    } else {
        NSLog(@"数据库打开失败");
    }
}

你可能感兴趣的:(2018-01-15)