长按手势删除tableView选中的行

首先要在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];
    }
    
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值