UItableView的刪除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//刪除數(shù)據(jù)庫數(shù)據(jù)
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中刪除一行坠非,重新點擊還會出現(xiàn)担扑。
數(shù)據(jù)庫中刪除
-(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];
//插入測試數(shù)據(jù)
NSString *deleteSQL = [NSString stringWithFormat:@"delete from wordbook where id = %d;",bookid];
[db executeUpdate:deleteSQL];
callback(1);
[db close];
} else {
NSLog(@"數(shù)據(jù)庫打開失敗");
}
}