3
votes

I have a UICollectionViewController that maintains a recipes collection.

class RecipesViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource  {

And another UICollectionViewController for maintaining results of the search, also implementing the UISearchResultsUpdating like that:

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

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

var searchController: UISearchController!

also initiated like that:

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

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

I placed into the header of RecipesViewController the UISearchBar from searchController.

My problem is that SearchResultsController is not updating when text is entered into searchBar. Method updateSearchResultsForSearchController is not even called.

2

2 Answers

2
votes
@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's 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 that this example solves your problem.

0
votes

Here is a lazy instantiation 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 that help!