Use existing tableView and ViewController using UISearchDisplayController

I have an application that usually lists Guilt throughout the application (full screen or in smaller UIViews). I usually call this ViewController, highlighting the view, assigning the / etc delegate to my main ViewController, and then populating the data through this delegate.

From the main view that wants to display the fault 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 

This is great, but now I am creating a search aspect, and I want to use the same Viewcontroller to display the results, as I have some complex rules in cellForRowAtIndexPath, etc.

However, UISearchDisplayController wants to use its own tableView, which would mean that I would have to duplicate all my code from WineListViewController (heightForRow, cellForRow) just to display the same path. Is there a way to connect my WineListViewController to a UISearchDisplayController so that I don't duplicate the code?

+6
source share
1 answer

Yes.

You need to set searchResultsDataSource and searchResultsDelegate from the UISearchDisplayController to point to an instance of your WineListViewController (you could do it in the appropriate place in WineListViewController and just pass the "I." These two UISearchDisplayController properties are exactly the same as the dataSource and UITisableView delegate properties of the UITisViewView. present a UITableView above your original UITableView and using these properties, you can specify which results should be displayed for a specific search string (as well as your source table). It is reloaded each time the line will be changed, and will be called by your data UATableView and delegation techniques.

You can distinguish between your standard UITableView and searchResultsTableView in your data sources and delegate methods to 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 searchBar search string. } } 

You should take a look at the delegate methods of the UISearchDisplayController, as you can implement them to indicate when the search display result table should be updated, etc.

+2
source

Source: https://habr.com/ru/post/894997/


All Articles