简体   繁体   English

检测UITableViewCell子视图内的触摸

[英]Detecting touch inside UITableViewCell subview

I am unclear where I should add the UIGestureRecognizer code to corresponding subviews of a UITableViewCell. 我不清楚在哪里应该将UIGestureRecognizer代码添加到UITableViewCell的相应子视图中。 I have read all the related questions I could find. 我已经阅读了所有可以找到的相关问题。 Right now my cells and cell's subviewsare generated inside of cellForRowAtIndexPath. 现在我的cell和cell的子视图在cellForRowAtIndexPath中生成。 I have tried to add the Gesture inside of cellForRowAtIndexPath with this: 我试图用cellForRowAtIndexPath添加Gesture:

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

However, this detects nothing. 但是,这没有任何检测。 To verify my UIGesture recognizer is working I have used the above code on the tableView itself, and it does register touches as expected. 为了验证我的UIGesture识别器是否正常工作,我在tableView本身上使用了上面的代码,它确实按预期注册了触摸。 Furthermore, when the tableView has the above gesture attached the below code is also being called as expected: 此外,当tableView附加了上述手势时,下面的代码也按预期调用:

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

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

I have tried to remove the GestureRecognizer from the tableView and inside of cellForRowAtIndexPath I have tried to attach the GestureRecognizer to the cell itself, any of its subviews, nothing else gets a touch detected. 我试图从tableView和cellForRowAtIndexPath中删除GestureRecognizer我试图将GestureRecognizer附加到单元格本身,任何子视图,没有其他任何东西检测到触摸。 (None of the above code is triggered) (以上代码均未触发)

Clearly I am adding the GestureRecognizer incorrectly. 显然我错误地添加了GestureRecognizer。 Where/When would be an appropriate location/time to add the GestureRecognizer? 何时/何时是添加GestureRecognizer的合适位置/时间?

Thank you. 谢谢。

I've done similar thing, but it was UILongPressGestureRecognizer . 我做过类似的事情,但它是UILongPressGestureRecognizer I think there is no big difference (because all touches are received by UITableView ). 我认为没有太大的区别(因为所有的触摸都是由UITableView接收的)。 I've added gesture recognizer in controllers viewDidLoad method (NOT IN cell). 我在控制器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];
    }
}

You can change Long press to regular one and try it yourself 您可以将长按更改为常规按并自行尝试

I needed to detect touches on different subviews inside my cell. 我需要检测单元格内不同子视图的触摸。 also handling iOS 9's UITableViewCellContentView . 还处理iOS 9的UITableViewCellContentView

First I overrided touchesBegan inside the my custom UITableViewCell 首先,我在我的自定义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];
}

And to fix support in iOS 9 we should override awakeFromNib or just disable the cell user intercations somehwere else if cell is not in Storyboard / xib: 要修复iOS 9中的支持,我们应该覆盖awakeFromNib,或者如果单元格不在Storyboard / xib中,则只禁用单元格用户交互。

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

of course we shouldn't forget to set our label user interactions enabled. 当然,我们不应该忘记设置我们的标签用户交互启用。

Not sure exactly what you are trying to do. 不确定你到底想要做什么。 If you just want to detect if the user taps on a cell within the table then you don't need to implement a gesture recognizer. 如果您只想检测用户是否点击了表格中的单元格,那么您不需要实现手势识别器。 Just implement the delegate method below to detect when a row from the table has been selected then process the elements of the row such as getting the subview, etc. 只需实现下面的委托方法来检测何时选择了表中的 ,然后处理行的元素,例如获取子视图等。

- (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];
}

If you describe your problem a little further then perhaps I can offer additional suggestions. 如果你进一步描述你的问题,那么也许我可以提供额外的建议。

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

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