长按UITableView

我想按一下UITableViewCell以打印“快速访问菜单”。有人已经这样做了吗?

特别是手势识别上UITableView


拉风的咖菲猫
浏览 894回答 3
3回答

明月笑刀无情

首先将长按手势识别器添加到表格视图中:UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]   initWithTarget:self action:@selector(handleLongPress:)];lpgr.minimumPressDuration = 2.0; //secondslpgr.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可能会触发多次(这是由于手势识别器状态更改)。

DIEA

我已经使用了安娜·卡列尼娜(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);            }        }    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS