Uisearchdisplaycontroller - How to Preload Searchresulttableview

How to preload or initialize a search using UISearchDisplayController?

You need something like this:

- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
if(lastSearch.length > 0) {
NSLog(@"In searchDisplayControllerDidBeginSearch");
[self.searchDisplayController.searchBar setText:lastSearch];
}
}

If you need to make further changes, I suggest opening up the UIKit headers UISearchBar.h and UISearchDisplayController.h and looking at what you can overload and where. Also, NSLog is your friend =)

Also

Just to explain what is going on and how this is different from ...searchBar.text = lastSearch, setText forces textDidChange to register, which in turn redoes the search, and subsequently has a call to shouldReloadTableForSearchString. Just changing the text without using setText (for some reason) doesn't trigger textDidChange.

Show a preloaded search results?

I finally found a way to do this.

I found out that the searchDisplayController simply removes the searchResultsTableView from the superview, so I just added the table view back into the superview whenever the display controller tried to hide the table view:

- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
{
// add the tableview back in
[self.view addSubview:self.searchDisplayController.searchResultsTableView];
}

and then I also have to show the tableview the first time the searchbar is clicked, so I did:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
// after the data has been preloaded
self.searchResults = self.allItems;
[self.searchDisplayController.searchResultsTableView reloadData];
}

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
[self.view addSubview:self.searchDisplayController.searchResultsTableView];
}

For me, 'allItems' is where I stored all the searchable items and 'searchResults' is where the filtered items (after the search) is stored. And of course, you would have to preload the items (e.g. search history) before reloading the data.

I don't know if this is a nice way or not to do it in terms of the performance and what not, but it worked perfectly for me, and I hope this could be useful for other people as well. Please comment if there is a better way to do this.

uisearchdisplaycontroller preload search results and search bar

So after a lot of brute force, I finally figured out a workable solution.

Here's my setup: I have two table views - T1, T2. T1's data is local and T2 fetches data from internet. When user searches in T1, and there are no results, user can tap on a button to perform the same search on T2. So now when T2 comes up, it'll show the navigation item by default - and this is something I couldn't change.

In -[T2 viewDidAppear:]

    [self.searchDisplayController setActive: YES animated: NO];
self.searchDisplayController.searchBar.text = self.pendingSearchTerm;
self.searchDisplayController.searchResultsTableView.hidden = YES; // so that we don't see "no results"

When data for T2 is ready:

[self.tableView reloadData];
self.searchDisplayController.searchBar.text = self.searchDisplayController.searchBar.text; // to reload search
self.searchDisplayController.searchResultsTableView.hidden = NO;

Hopefully this helps someone. If you are able to figure out a way to hide the navigation bar initially, that'd be solve the rest of my problem.

How to control the searchResultsTableView of a UISearchDisplayController?

You have a TableViewDataSource property on the UISearchDisplayController, this data source produces the UITableViewCell. Implement the UITableViewDataSource protocol and you'll have the control over the customization of the tableView in the method :

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

Show UISearchDisplayTableView when search string is empty

Well the solution to this is to not use the UISearchDisplayController. It simply doesn't offer the flexibility you need to do something like this. Build your own custom search interface with a UISegmentedControl, UISearchBar and UITableView for maximum flexibility.



Related Topics



Leave a reply



Submit