长按手势删除tableView选中的行

来源:互联网 发布:pdf sql pan 编辑:程序博客网 时间:2024/06/08 17:18

首先要在viewDidLoad中注册长按手势

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressToDo:)];
    
    longPress.minimumPressDuration = 2.0;   //长按2秒会触发长按手势
    
    [self.myTableView addGestureRecognizer:longPress];



- (void)longPressToDo:(UILongPressGestureRecognizer *)gesture

{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        CGPoint point  = [gesture locationInView:self.myTableView];
        selectedIndexPath = [self.myTableView indexPathForRowAtPoint:point];
        
        if (selectedIndexPath == nil) {
            return;
        }
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"删除选中" message:@"是否删除" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
            [alertView show];
        }
        
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{       //UIAlertView代理方法

    if (buttonIndex == 0) {
        [self deletePost];
        [array removeObjectAtIndex:selectedIndexPath.row];     //将数据源array中的对于元素删除
        NSArray *arrayPath = [NSArray arrayWithObject:selectedIndexPath];
        [self.myTableView deleteRowsAtIndexPaths:arrayPath withRowAnimation:UITableViewRowAnimationLeft];   //把tableView中的对应cell行删除
    }
    
    
}


- (void)deletePost                                            //将数据库中的对应数据删除
{
    Word *word =(Word *)[array objectAtIndex:selectedIndexPath.row];
    
    FMDatabase *db = [FMDatabase databaseWithPath:[self getPath]];
    
    
    
    
    if ([db open]) {
        NSLog(@"在删除数据时数据库打开成功");
        
        
        NSString *sql = [NSString stringWithFormat:@"delete from Words where danci = '%@' ",word.danci];
        
        BOOL res = [db executeUpdate:sql];
        
        if (res) {
            NSLog(@"---------delete SUCCESS!--------");
            
            
        }
        [db close];
    }
    
}
 
0 0