繁体   English   中英

检测UITableViewCell子视图内的触摸

[英]Detecting touch inside UITableViewCell subview

我不清楚在哪里应该将UIGestureRecognizer代码添加到UITableViewCell的相应子视图中。 我已经阅读了所有可以找到的相关问题。 现在我的cell和cell的子视图在cellForRowAtIndexPath中生成。 我试图用cellForRowAtIndexPath添加Gesture:

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[mySubview addGestureRecognizer:tapGesture];
tapGesture.cancelsTouchesInView = YES;
tapGesture.delegate = self;

但是,这没有任何检测。 为了验证我的UIGesture识别器是否正常工作,我在tableView本身上使用了上面的代码,它确实按预期注册了触摸。 此外,当tableView附加了上述手势时,下面的代码也按预期调用:

-(BOOL) gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    NSLog(@"shouldRevceiveTouch");
    return YES;
}

- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UITapGestureRecognizer *)otherGestureRecognizer 
{
    NSLog(@"simultaneously");
    return YES;
}

我试图从tableView和cellForRowAtIndexPath中删除GestureRecognizer我试图将GestureRecognizer附加到单元格本身,任何子视图,没有其他任何东西检测到触摸。 (以上代码均未触发)

显然我错误地添加了GestureRecognizer。 何时/何时是添加GestureRecognizer的合适位置/时间?

谢谢。

我做过类似的事情,但它是UILongPressGestureRecognizer 我认为没有太大的区别(因为所有的触摸都是由UITableView接收的)。 我在控制器viewDidLoad方法(NOT IN单元格)中添加了手势识别器。

- (void) tableViewLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.messageTableView];

    NSIndexPath *indexPath = [self.messageTableView indexPathForRowAtPoint:p];
    if (indexPath == nil)
        NSLog(@"long press on table view but not on a row");
    else {
        UITableViewCell *cell = [self.messageTableView cellForRowAtIndexPath:indexPath];
        CGPoint pointInCell = [cell convertPoint:p fromView:self.messageTableView];
    }
}

您可以将长按更改为常规按并自行尝试

我需要检测单元格内不同子视图的触摸。 还处理iOS 9的UITableViewCellContentView

首先,我在我的自定义UITableViewCell中覆盖了touchesBegan

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:touch.view];

    // Imagine I have 2 labels inside my cell
    CGPoint convertedPoint = [self.firstLabel convertPoint:point fromView:touch.view];
    if ([self.firstLabel pointInside:convertedPoint withEvent:nil]) {
        // Touched first label
        return;
    } 

    convertedPoint = [self.secondLabel convertPoint:point fromView:touch.view];
    if ([self.secondLabel pointInside:convertedPoint withEvent:nil]) {
        // Touched second label
        return;
    } 

    // no labels touched, call super which will call didSelectRowAtIndexPath
    [super touchesBegan:touches withEvent:event];
}

要修复iOS 9中的支持,我们应该覆盖awakeFromNib,或者如果单元格不在Storyboard / xib中,则只禁用单元格用户交互。

- (void)awakeFromNib {
    // Initialization code
    self.contentView.userInteractionEnabled = NO;
} 

当然,我们不应该忘记设置我们的标签用户交互启用。

不确定你到底想要做什么。 如果您只想检测用户是否点击了表格中的单元格,那么您不需要实现手势识别器。 只需实现下面的委托方法来检测何时选择了表中的 ,然后处理行的元素,例如获取子视图等。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   // Do all my cool tap related stuff here for example, get the row that was tapped:
   UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];
   // get your subview (assume its a UIImageView) from cell - one way to do it below
   UIImageView photo = (UIImageView *)[cell.contentView viewWithTag:PHOTO_TAG];
}

如果你进一步描述你的问题,那么也许我可以提供额外的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM