UISearchDiplayController: show all results without entering text?
I have 开发者_JS百科a UISearchDisplayController
that is being used to display a long list of options for the user to pick. I filter the list using the search bar supplied by the controller.
It all works ok, apart from I can't get the results table to display except by entering text into the search bar. I want to have the table show all the results I have, even when there is no text in the search bar. At present, when there is no text I just have a dimmed underlying view.
Is this possible please? Or should I not bother with the UISearchDisplayController
at all, and just have a searchbar in another tableview that I display, and then just pop it off the stack?
To display search results instantly with empty searchBar upon keyboard appearance, add the following code to ViewController:
-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
[controller.searchBar.delegate searchBar:controller.searchBar textDidChange:@" "];
}
To keep table showing results when users enter enters some text and then clears searchBox, add:
-(void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
[controller.searchBar.delegate searchBar:controller.searchBar textDidChange:@" "];
}
Don't forget to return YES in searchDisplayController:shouldReloadTableForSearchString:
Typically you would want to start by loading all of your content into a single array from which you would display a table view as "normal".
Once this is working, create a second array containing the search results from within
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
Then, within the table view's delegate/data source methods, decide which context you are in by checking
if (_tableView == self.searchDisplayController.searchResultsTableView)
精彩评论