KSoza
KSoza

Reputation: 321

SearchBar not displaying results until cancel button clicked

I have implemented a UITableView with search bar (and search display) - all works fine, but the table results do not get updated until the search bar cancel button is tapped.

Delegate methods:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    // asynchronous request with [self.tableView reloadData] in the connectionDidFinishLoading
    [self getProductData:searchBar.text]; 
 [searchBar resignFirstResponder];
 [self.tableView reloadData];


}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar  {  
} 

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
 return YES;
}

Do I need to call a delegate method after receiving the data from the server? Or should I make the request synchronous?

Thanks


edit: I tried with a synchronous request and it still does not work!

Upvotes: 2

Views: 1941

Answers (3)

KSoza
KSoza

Reputation: 321

Resolved this issue by adding this code to the end of my getProductData method:

[[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

Upvotes: 4

vikingosegundo
vikingosegundo

Reputation: 52237

Please try something like this:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self getProductData:searchText]; 
    [self.tableView reloadData];
}

Upvotes: 0

Benoît
Benoît

Reputation: 7427

I think you have to implement:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

and return YES

See the TableSearch sample code

Upvotes: 0

Related Questions