0

Hi I am developing small IOS application in which I want to display search bar and below it table view. In which I want to hide keyboard when user click outside. For that reason I am using tap recogniser but becoz of that my table view stops listening for row selection.

Here Is my code

//inside view did load 
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];

-(void)dismissKeyboard {
    [_searchBar resignFirstResponder];
}

but because of this my row selection of table view get disable. that mean didSelectRowAtIndexPath never get called. Any one have solution for this. Need Help. Thank you .

nilkash
  • 7,408
  • 32
  • 99
  • 176

4 Answers4

1

Try adding this line of code this will solve your problem..

tap.cancelsTouchesInView = NO;
Viper
  • 1,408
  • 9
  • 13
0

There is no need to use TapGestureRecognizer. Use SearchBarDisplayController

hope this will work for you

download a demo project

Community
  • 1
  • 1
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
0

You should implement UIGestureRecognizerDelegate and add the following:

//inside view did load

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];

// UIGestureRecognizerDelegate methods

#pragma mark UIGestureRecognizerDelegate methods

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isDescendantOfView:yourTableView]) {

        // Don't let selections of auto-complete entries fire the 
        // gesture recognizer
        return NO;
    }

    return YES;
}
Huy Nghia
  • 996
  • 8
  • 22
0

There is a "Search Bar and Search Display Controller" in the Utilities panel that sounds like it would be perfect for you. This is a good tutorial that explains how to implement it. This way you won't have the keyboard issue anymore.

trevorj
  • 2,029
  • 1
  • 16
  • 11