UIView "stealing" touch from UITableView

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)


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


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#>)

链接地址: http://www.djcxy.com/p/87688.html

上一篇: 在Storyboard中向UITableView添加一个子视图

下一篇: UIView从UITableView中“窃取”触摸