Why is the updateSearchResultsForSearchController method not being called?

I have a UICollectionViewController that maintains a collection of recipes.

class RecipesViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource  {

      

And another UICollectionViewController for saving search results, also implementing UISearchResultsUpdating as follows:

class SearchResultsController: UICollectionViewController, UISearchResultsUpdating,UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { ...

      

I need to search a collection, so I have a member in the RecipesViewController

var searchController: UISearchController!

      

also triggered like this:

let resultsController = SearchResultsController()
resultsController.recipes = recipes
resultsController.filteredResults = recipes

searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = resultsController

      

I have placed in the header RecipesViewController UISearchBar from searchController.

My problem is that SearchResultsController doesn't update when text is entered into searchBar. The updateSearchResultsForSearchController method is not even called.

+3


source to share


2 answers


@interface YourViewController_CollectionResults () <UISearchResultsUpdating>

@end

@implementation YourViewController_CollectionResults
- (void)viewDidLoad {

    [super viewDidLoad];

    // The collection view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
    UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"CollectionSearchResultsNavController"];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

    //It really import that you class implement <UISearchResultsUpdating> 
    self.searchController.searchResultsUpdater = self;

    self.tableView.tableHeaderView = self.searchController.searchBar;

    self.definesPresentationContext = YES;

}

      

You have an example in this github



Sample-UISearchController

I hope this example solves your problem.

+2


source


Here's a lazy instance for the search controller:

    self.searchController = ({
        // Setup One: This setup present the results in the current view.
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.hidesNavigationBarDuringPresentation = false
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.searchBarStyle = .Minimal
        controller.searchBar.sizeToFit()
        self.tableView.tableHeaderView = controller.searchBar
        return controller
    })()

      



I hope help!

0


source







All Articles