I am populating a UITableView with cells containing a UIImageView subview. I would like to detect touches on the image view, so I am also adding a gesture recognizer to it. However, nothing is getting called. Here is my code:
UIImageView *delete = [[UIImageView alloc] initWithFrame:CGRectMake(1.0, 8.0, 33.0, 33.0)];
delete.image = [UIImage imageNamed:@"Delete.png"];
delete.userInteractionEnabled = YES;
delete.contentMode = UIViewContentModeCenter;
UIGestureRecognizer *gesture = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(deleteTapped)];
gesture.delegate = self;
[delete addGestureRecognizer:gesture];
[self.cellView addSubview:delete];
Note: I am technically adding the image view to a container which is then added to the the cell itself, hence [self.cellView addSubview:delete]. But I have had the same results adding the image view to the cell directly.
According to this answer on a similar post this is a known bug in iOS 5.0; the solution is to override a delegate method to force the gesture recognizer to begin:
The fix is to override -gestureRecognizerShouldBegin: in the gesture recognizer’s delegate and return YES. This bug should be fixed in a future version of iOS 5.x. This is only safe as long as you are not using the new UITableViewCell copy/paste API’s.
I’ve tried this, but the delegate method never gets called. I’ve set the delegate property (see above) and implemented <UIGestureRecognizerDelegate> as follows:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
NSLog(@"gesture delegate method called");
return YES;
}
Does any one have an idea why this still wouldn’t be working? Or perhaps what this status of this bug is in iOS 5.1?
try to return YES for this delegate methode
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;and if you want to detect the tap action use
UITapGestureRecognizer