为了账号安全,请及时绑定邮箱和手机立即绑定

长按UITableView

长按UITableView

iOS
拉风的咖菲猫 2020-02-04 15:42:35
我想按一下UITableViewCell以打印“快速访问菜单”。有人已经这样做了吗?特别是手势识别上UITableView?
查看完整描述

3 回答

?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

首先将长按手势识别器添加到表格视图中:


UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 

  initWithTarget:self action:@selector(handleLongPress:)];

lpgr.minimumPressDuration = 2.0; //seconds

lpgr.delegate = self;

[self.myTableView addGestureRecognizer:lpgr];

[lpgr release];

然后在手势处理程序中:


-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer

{

    CGPoint p = [gestureRecognizer locationInView:self.myTableView];


    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];

    if (indexPath == nil) {

        NSLog(@"long press on table view but not on a row");

    } else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {

        NSLog(@"long press on table view at row %ld", indexPath.row);

    } else {

        NSLog(@"gestureRecognizer.state = %ld", gestureRecognizer.state);

    }

}

您必须注意这一点,以免干扰用户对单元格的正常轻敲,并注意handleLongPress可能会触发多次(这是由于手势识别器状态更改)。


查看完整回答
反对 回复 2020-02-04
?
DIEA

TA贡献1820条经验 获得超2个赞

我已经使用了安娜·卡列尼娜(Anna-Karenina)的答案,并且在出现严重错误的情况下效果很好。


如果您使用的是节,则长按节标题将导致您在按该节的第一行时得到错误的结果,我在下面添加了一个固定版本(包括根据手势状态过滤虚拟呼叫, Anna-Karenina的建议)。


- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer

{

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {


        CGPoint p = [gestureRecognizer locationInView:self.tableView];


        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];

        if (indexPath == nil) {

            NSLog(@"long press on table view but not on a row");

        } else {

            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

            if (cell.isHighlighted) {

                NSLog(@"long press on table view at section %d row %d", indexPath.section, indexPath.row);

            }

        }

    }

}


查看完整回答
反对 回复 2020-02-04
  • 3 回答
  • 0 关注
  • 889 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信