Question

I want to detect whenever the user taps the remaining space in the UITableView below the tableViewCells.

My primary purpose is that I want to resign response to a searchBar when user taps the empty TableView area.

It is not responding to UITouch Events. I also tried adding UITapGestureRecognizer, but it had covered the entire tableview.

Any help will be much appreciated.

Was it helpful?

Solution

In addition to adding the UITapGestureRecognizer, you need to implement a delegate method and and assign the delegate to the gesture recognizer. First somewhere in your view controller:

UITapGestrueRecognizer *tapRecognizer = ...;
tapRecognizer.delegate = self;

And here is the delegate method to implement:

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    CGPoint location = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = self.tableView indexPathForRowAtPoint:location];
    return (indexPath == nil);
}

The delegate method checks if the tap occurred in the cells or not, and if not, it doesn't start recognizing the tap in order to give the table view the chance to interpret the gesture.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top