Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use existing tableView and ViewController with UISearchDisplayController

I have an app that commonly lists Wines throughout the app (full screen or in smaller UIViews). I call this ViewController normally, by allocating the View, assigning the delegate/etc to my main ViewController, and then populating the data via that delegate.

From a master View that wants to display wines on the screen:

wineListViewController = [[WineListViewController alloc] initWithWines:nil];
wineListViewController.navigationController = self.navigationController;
self.winesTableView.dataSource = wineListViewController;
self.winesTableView.delegate = wineListViewController;
// Table population code goes here

That's great but now I'm building a Search aspect, and I want to use this same Viewcontroller for displaying the results since I have some complex rules in cellForRowAtIndexPath and such.

However, the UISearchDisplayController wants to use its own tableView, which would mean I'd have to duplicate all my code from WineListViewController (heightForRow,cellForRow) just to display the same way. Is there a way I can connect my WineListViewController into the UISearchDisplayController so I'm not duplicating code?

like image 639
beeudoublez Avatar asked Aug 13 '11 23:08

beeudoublez


1 Answers

Yes.

You need to set the searchResultsDataSource and searchResultsDelegate of the UISearchDisplayController to point to the instance of your WineListViewController (you could do this in an appropriate place in the WineListViewController and simply pass 'self'. These two properties of the UISearchDisplayController are exactly the same as the properties dataSource and delegate of a UITableView. The UISearchDisplayController is going to present a UITableView over your original UITableView, and using these properties, you can specify which results should be displayed for a particular search string (just as you would your original table). The search results table view should be reloaded every time the string is changed, and your UITableView datasource and delegate methods will be called.

You can differentiate between your standard UITableView and the searchResultsTableView in your datasource and delegate methods o the WineListViewController as follows (for example):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView)
    {
        // Return a standard listing for indexPath.
    }
    else if (tableView == self.searchDisplayController.searchResultsTable)
    {
        // Return a filtered cell, based on a filtered model of the searchDisplayController's searchBar's search string.
    }
}

Y should take a look at the delegate methods of UISearchDisplayController, as you can implement those to specify when the search display results table should be refreshed and so on.

like image 191
James Bedford Avatar answered Nov 22 '22 09:11

James Bedford