5

I have a custom tableviewCell with an UIImageView imgView inside. I added UITapGestureRecognizer to imgView, so that I can change image of imgView whenever I tap to it. However, when I tap to imgView, didSelectRowAtIndexPath is also triggered. What I want is:

  • When tapping outside imgView: Trigger didSelectRowAtIndexPath
  • When tapping inside imgView: Block didSelectRowAtIndexPath, just call gesture callback to change image of imgView.

Callback function is already called when I tap inside imgView but I can not block didSelectRowAtIndexPath in that case. I've been searching around for hours but have not found the solution yet. I'm using . Anyone have any idea for my problem? Thank all!

Update

Finally I found out what my mistake is.

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onFavouriteIconTapped:)];


tapGesture.cancelsTouchesInView = NO; // this should be YES by default

[_ivImageView addGestureRecognizer:tapGesture];

Just set cancelsTouchInView = YES fix my problem. If the value is YES, touch event will be consumed by ivImageView and cell wont be selected. Hope this will help someone who has the same problem.

meaholik
  • 440
  • 5
  • 19
  • 3
    Try adding UIButton and actions instead of UITapGesture, and UIImageView. – iphonic Aug 03 '16 at 09:41
  • 1
    Add the tag of the language you are using. Obj C or Swift. – NSNoob Aug 03 '16 at 09:45
  • This should help [UIGesture & UITableView](http://stackoverflow.com/questions/4604296/uigesturerecognizer-and-uitableviewcell-issue) – gurmandeep Aug 03 '16 at 10:02
  • aad UIButton on imageView and write your code in action method @iphonic said – Pranav Aug 03 '16 at 10:05
  • I updated my question. My problem is not with calling gesture function. That function is already called. My problem is: how can I block didSelectRowAtIndexPath just in case tapping inside imgView (still keep didSelectRowAtIndexPath if tapping outside imgView) – meaholik Aug 04 '16 at 02:54

2 Answers2

0

Your approach is fine. Your tap gesture selector is not getting called probably because you might have not enabled user interaction of the UIImageView.

imageView.userInteractionEnabled = TRUE;

Default value of userInteractionEnabled is NO, for UIImageView.

Once your selector starts getting called, you will get the desired behaviour i.e on click of imgView, tap gesture selector gets called and on touch outside the imgView didSelectRowAtIndexPath will get called. This is in accordance to iOS responder chain rules.

SHN
  • 785
  • 7
  • 23
  • Thanks for your answer. But my problem is when tapping INSIDE imgView, didSelectRowAtIndexPath is also called. How can I block it if tapping inside (still keep didSelectRowAtIndexPath when tapping outside imgView) – meaholik Aug 04 '16 at 02:58
  • @meaholik: Can you share some of your code snippet? – SHN Aug 04 '16 at 09:44
0

@SHN ,you are saying right. May be @meaholik , you have missed that line.

imageView.userInteractionEnabled = TRUE;

You can also check this link.

Hope it will help you. :)

Community
  • 1
  • 1
Amir Khan
  • 1,318
  • 1
  • 14
  • 39
  • Thanks for your answer but it's not the problem here. I updated my question. Please take a look at it. – meaholik Aug 04 '16 at 02:56
  • Ok @meaholik. Thank you so much for updating me. Can you provide that code where you are going to add the tap gesture on UIImageView? – Amir Khan Aug 04 '16 at 05:52