0

I have an autocomplete table that I create the frame for when the user types anything in a search bar button item. The problem is, I want to be able to dismiss the tableview when the user touches outside of it or the table, so naturally I added a tap touch recogniser to the main view.

Problem is, the view "hijacks" the touches from the table, so when you try to touch the table it dismisses it (which makes perfect sense as the table is a subview of that UIView)

Anyone have any smart solutions? (My initial hunch is to put the UITableView in a new window on top of the view's window, but I would prefer if there was something more elegant)

1
  • take your tableview to another class, define touch event for it there and add that class as a subview to first one Dec 3, 2013 at 11:53

2 Answers 2

4

Maybe you can try

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if ([touch.view.superview.superview isKindOfClass:[UITableView class]]) return NO;


    else return YES;
}

//Init recognizer

UIGestureRecognizer *gestureRecognizer;
gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[gestureRecognizer setCancelsTouchesInView:NO];
[self.view addGestureRecognizer:gestureRecognizer];
self.tapRecognizer = (UITapGestureRecognizer *)gestureRecognizer;
gestureRecognizer.delegate = self;
[gestureRecognizer release];

and put some stuff for your gesture in

- (void)handleTap:(UITapGestureRecognizer *)recognizer {

}

Hope it helps :)

1
  • 1
    The view when you press a tableview is actually UITableViewContentView so if you want to get to a kind of class UITable view its touch.view.superview.superview. But now that I did that it works perfectly. thanks for a quick and simple answer.I accepted, just please edit the correction. Dec 3, 2013 at 9:04
0

may be you can try

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

methods for table view selection of row user will feel that user selected the value and for

and for outside the table use uiviews touch begin methods and check for rect CGRectContainsPoint(<#CGRect rect#>, <#CGPoint point#>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.