0
votes

I have implemented UITableView where each cell contains some buttons In order to detect if user taps on table I have added UITapGestureRecognizer.

I want the buttons in the cells to do nothing when user taps on them. I have implemented this selector:

- (void) backgroundTouched:(UITapGestureRecognizer*)sender {
  UIView *view = sender.view;
  NSLog(@"backgroundTouched %@", view);
}

I see that when I tap anywhere on the table this selector gets called except if I tap on any of the buttons contained in the UITableViewCell.

How can I avoid that?

Here is the code which creates the UITapGestureRecognizer:

pragma mark UISearchControllerDelegate

- (void)didPresentSearchController:(UISearchController *)searchController
{
  NSLog(@"didPresentSearchController");
  self.cancelGesture = [UITapGestureRecognizer new];
  [self.cancelGesture addTarget:self action:@selector(backgroundTouched:)];
  self.cancelGesture.cancelsTouchesInView = YES;
  [self.tableView addGestureRecognizer:self.cancelGesture];
}
2
Pl. clear your concern, you want to disable tap on table or button? - Krunal
Disable button. Right now when I tap on button button action gets called and gesture selector does not. I want the other way around - RuLoViC

2 Answers

1
votes

If you don't want your buttons to do anything, disable the userInteractionEnabled of the button.

cell.addCashButton.userInteractionEnabled = NO;
0
votes

My suggestion:

Table has own touch action facility use it's delegate method didSelectRowAtIndexPath

And if you are using UIButton in custom cell then instead of UITapGestureRecognizer you can add button action method in cellForRowAtIndexPath with button's tag like below

in cellForRowAtIndexPath method

cell.yourButton.tag = indexPath.row
[cell.yourButton addTarget:self  action:@selector(myButtonActionMethod:)  forControlEvents:UIControlEventTouchUpInside]

And declare button action method

-(void) myButtonActionMethod:(UIButton*)sender
 {
    NSLog(@"you clicked on button %@", sender.tag);
 }