UITableViewCell tap on subview without cell selection - ios

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 objective-c. 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.

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 ,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. :)

Related

UITableView cancels UITapGesture

uitableviewdidselect cancel outs tap gesture action
there is an UIImageView in cell.contentView and there is a tap gesture to enlarge the image the control is not going to the Tap Gesture action its passing to the tableview didselect delegate ? I am using UITableView class for my table already did userInteractionEnabled=YES & cancelsTouchesInView = NO
Make sure you set
tapGestureRecognizer.cancelsTouchesInView = NO;
It won't work because table view can't take two user interactions at a time. First it will give priority to didSelectRowAtIndexPath. You need to disable user interaction on cells. So that tap gesture will be called. Make sure that user interaction enabled for imageView.

UIImageView won't recognize tap gesture

I've searched through other questions similar to this on SO already, and came up empty with a solution to my problem. For some reason, my UIImageView (which I've added to a UIView in storyboard) just won't recognize a tap gesture I've created. User Interaction is enabled, as is multiple touch. My selector method just never gets called.
self.image is my imageView property that I've added onto a UIView.
- (void)viewDidLoad {
[super viewDidLoad];
self.image.image = [UIImage imageNamed:#"mountainWallpaper.png"];
[self gesture];
}
-(void)gesture {
UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(imageTapped:)];
//imageTap.numberOfTapsRequired = 1;
//imageTap.numberOfTouchesRequired = 1;
imageTap.delegate = self;
[self.image addGestureRecognizer:imageTap];
}
-(IBAction3)imageTapped:(UIGestureRecognizer *)tap {
NSLog(#"test");
}
Any suggestions? I've looked through most other questions I found and tried out various solutions with no such luck. Many answers said to verify that user interaction is enabled, to make sure the delegate is set...I've tried much of what has been suggested already.
I've also experimented with first adding the UIView as the tap gesture target, adding the imageView itself as the target, adding the gesture to both the UIView and the imageView...nothing.
In storyboard, I have my UIImageView connected to my viewController as both the view and the image. I generally do things programmatically so I have to get used to connecting objects in IB, I'm pretty bad at it.
Check for following points may be it will helpful for you :
Make sure your UIImageView User interaction is enable .
Image View priority should be on top over super view .
make sure super view of Image view userIntration is also enable .
have you added UI gesture Delegate ?
It looks like you have a spelling mistake. Try changing IBAction3 to IBAction or void:
-(void)imageTapped:(UIGestureRecognizer *)tap {
NSLog(#"test");
}

How do I make a UILabel *not* pass touches to the UITableViewCell it's embedded in?

I have a UITableView with a bunch of cells, and I add a UILabel to each. I want the UILabel, when tapped, to accept the touches, not the cell. Obviously if they tap outside of the label and still on the cell that is fine, but if it's on the label the cell should not cause a segue or action or whatever, only the label's action on tap should fire.
My normal method to cause this is to set the UIView's userInteractionEnabled to YES, but in this case when I set it on the label it doesn't cause anything different to happen. When I watch touchesBegan in the label subclass those methods do fire, but the cell selection does as well.
How do I make the UILabel not pass the touches on to the cell?
I recommend adding a UITapGestureRecognizer to the label and setting cancelsTouchesInView property to YES to prevent the touches from being delivered to the view upon gesture recognition, ex:
UITapGestureRecognizer *labelTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
labelTapGesture.cancelsTouchesInView = YES;
[label addGestureRecognizer:labelTapGesture];
label.userInteractionEnabled = YES;
And since you don't want the gesture to perform an action, you can set its action to nil.

UITableViewCell subview does not receive touch

My reusable cell contains a view with all its informations (UIImageView, UILabel, etc.) with a frame of 0,0,320,63, named mainView
I also have another subview, added programmatically, with a frame of -160,0,160,63, named leftView, contained in mainView.
I added a UISwipeGestureRecognizer to the cell, so when you swipe, I change the frame of mainView to 160,0,320,63. It works perfectly.
I just have a problem, in leftView, I have some UIImageView with userInteractionEnabled set to YES, with a gesture recognizer on it. But this gesture recognizer is never fired, it still calls the -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method.
I looked at this SO answer, but it does not help me.
Would somebody help me with that ?
Thank you !!
EDIT : Here is my gesture recognizer instantiation
// Selector is valid, I checked it out. _viewController too.
UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
[imageView setImage:someImage];
[imageView setUserInteractionEnabled:YES];
[_leftView addSubview:imageView];
UITapGestureRecognizer * gr = [[UITapGestureRecognizer alloc] initWithTarget:_viewController action:selector];
[imageView addGestureRecognizer:gr];
Without seeing the gestureRecognizer's initialization, it could be a number of things that are the problem. Two main things to look into however:
1.) Have you set the cancelsTouchesInView flag? Normally this would be messing with your tableView touches, but you should look into it though.
2.) Have you verified the subview layout inside of storyboard? Is there something blocking part of your imageView?
-Also you've actually allocated the gestureRecognizer correct?
Have you tried to set table cell selectable to No? in this case your swipe gesture should still work, but table deligate method for cell selection shouldn't.
I figured it out myself.
The problem was because the leftView frame was outside of the mainView frame (-160 in x).
To solve my problem, I changed mainView in storyboard, so that it also starts at -160. And instead of adding leftView at -160 in x, I add it at 0.
Thanks anyway !

UIGestureRecognizer handle touches in UITableView

I have an UITableView in my class with a custom UITableViewCell with an UIImageView inside.
I need to detect the touch on this UIImageView to handle a custom method(handleSingleTouch:)
and I need to call the -didSelectRowAtIndexPath when the cell is selected.
So i added a UIGestureRecognizer to my TableView in this way:
UIGestureRecognizer *GEST=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTouch:)];
[self.myTable addGestureRecognizer:GEST];
GEST.delegate=self;
[GEST release];
[GEST setCancelsTouchesInView:NO];
[GEST setDelaysTouchesEnded:NO];
There is the conflict:
every time the UIImageView is touched the tableview the GestureRecognizer start the method but also the didSelectRowAtIndexPath is called.
Someone know the way to call the gestureRecognizerMethod without call the didSelectRowAtIndexPath?
Try adding the gesture recognizer to the image rather than the table. See this answer https://stackoverflow.com/a/7760402
I can't think of a way to do that, as you need to be touch responsive in the whole cell. But what you can do is tag your UIImageView and then check at the beginning of -didSelectRowAtIndexPath, for that UIImageView and return.

Resources