ios 实现在tableViewCell上面添加长按手势 删除该条cell以及列表后台数据等

自己的代码  需要   把属性更改成自己要使用的

//创建长按手势

    在cellForRowAtIndexPath代理方法中

                UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpGR:)];

                //设定最小的长按时间 按不够这个时间不响应手势

                longPressGR.minimumPressDuration = 1;

                [cell addGestureRecognizer:longPressGR];

//实现手势对应的功能

-(void)lpGR:(UILongPressGestureRecognizer *)lpGR

{

    if (lpGR.state == UIGestureRecognizerStateBegan) {//手势开始

        CGPoint point = [lpGR locationInView:self.tbFirst];

        self.index = [self.tbFirst indexPathForRowAtPoint:point]; // 可以获取我们在哪个cell上长按

        self.indexNum = self.index.row;

    }

    if (lpGR.state == UIGestureRecognizerStateEnded)//手势结束

    {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否删除" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];

        [alert show];

    }

}

 //提示框代理方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 0) {

        NSDictionary * dic = self.hopeDs[self.indexNum];

        NSString *url = [NSString stringWithFormat:@"%@%@",JJBaseUrl,@"/v1/infomarket/delPost"];

        NSMutableDictionary *params = [NSMutableDictionary dictionary];

        params[@"postId"] = dic[@"id"];

        params[@"ticket"] = [JJUserInfoModel userInfoFromFile].ticket;

        [JJHttpTools deleteUrl:url params:params success:^(id json) {

            [JJMBProgressTool showSuccessProgressViewWithText:@"已删除"];

            [self.hopeDs removeObjectAtIndex:self.indexNum];

            //删除列表内容

            [self.tbFirst deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.index] withRowAnimation:UITableViewRowAnimationFade];

        } failure:^(NSError *error) {

            

        }];

    }else{

        

    }

}

 

posted @   孙富有(iOS工程师)  阅读(8410)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示