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.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With