Question

I am presently using a default configuration of the UISearchDisplayController build in Storyboard, where I dragged the UISearchBar and SearchDisplayController onto an existing UITableViewController. Because it was dragged onto the TableViewController, there are two tables that I am managing - the self.tableView, and self.searchDisplayController.searchResultsTableView.

What I would like to accomplish is to only show the tableView as the default table when a user opens this view, as well as reflected to show the user's search. As illustration (taken from a RW tutorial):

enter image description here

What I would want to achieve is that, as you are typing the word "egg", the tableView of RecipeBookVC would get rid of the rows without "egg", so you are left with the results viewed in self.tableview without having to see it from the resultsTableView. (Preferably also without hiding the Navigation Bar, so the search feels more like a "filter" rather than a search)

Thank you!

Was it helpful?

Solution

I don't think you actually need a Search Display Controller here. All you need is a table view (probably a UITableViewController) and UISearchBar. The table view displays the contents of an array containing the data. Whenever the search query changes, you update that array accordingly, and reload the tableView to display the updated array data. This will give you the filtering feel that you want.

To display the data in the table view you'd use

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

and to get the query as the user types you'd use

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

After you've filtered the array that is the data source, you call [self.tableView reloadData].

I went ahead and whipped together a sample project with the methods implemented, so it's a little more clear what I mean. Hope this works out for ya.

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