Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView dismisses keyboard on reloadData in one place, but not in another?

I'm sifting through pre-existing code from a new job I just started, and one of the first bugs I'm being confronted with is a search bar attached to a UITableView subclass, which is dismissing the keyboard after every keystroke. After some searching I found this SO question, and it seemed I'd found my answer: the app was using reloadData to refresh the results after every keystroke, and that reload was calling resignFirstResponder and dismissing the keyboard. Frustrating, but explained and understood.

But then my boss pointed out that we use almost the exact same code elsewhere in the app, and it works perfectly. How could that be? I find myself in the unusual position of needing to explain why something DOES work, since the above link would indicate that dismissing the keyboard during reloadData is the "normal" behavior.

Below are the relevant methods from the two ViewControllers, each of which is subclassed from the same custom UITableView. (The methods have been simplified, but the differing behavior remains.) A part of me thinks that it must be something buried in difference between the two subclasses, but then if reloadData is the problem and it isn't being overridden anywhere, doesn't that mean the subclasses are irrelevant?

Lookup View:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self.tableView reloadData];
    NSLog(@"Lookup Reloaded");
}

Search View:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self.tableView reloadData];
    NSLog(@"Search Reloaded");
}

As you can see, they are identical. Both are successfully called after each keystroke, and yet the former dismisses the keyboard and the latter does not.

Each of these View Controllers is over a thousand lines long, so I'm having a hard time narrowing down what might be causing the differing behavior. Can someone who understands FirstResponders and TableView reloads help give me some leads on where to look? It seems like a fairly simple process (a key is pressed > the method is called > it reloads the table > keyboard is dismissed in the process) without much room to alter that behavior, but obviously there is a way to prevent that final step because the Search View is already doing it.

Any idea where I should focus my search to figure out why?

like image 271
Nerrolken Avatar asked Oct 02 '14 22:10

Nerrolken


1 Answers

Check to see if this method has been implemented on either of the delegates for this UISearchBar.

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;

If this method returns NO, it will prevent the search bar from resigning the first responder.

like image 69
dfmuir Avatar answered Nov 14 '22 22:11

dfmuir