I have been struggling a lot with this problem. Some posts deal with this problem but no solution has emerged in my case.
Here is the problem: I would like to display some results with the UISearchController method. My original table view with a custom cell works perfectly in the sense that all my entries are here and it displays as I want.
When I'm using a search bar with scope buttons, again everything is working perfectly, my filtered results are correctly stored in a list and even my custom cell get everything it needs, in the sense that when I check the cell attributes, it corresponds to the filtered entries...
But where I go crazy is that the "new" table view (the one with the results) that is displayed is empty (in the sense that all the view is filled with empty lines) despite the fact the filtered entries are here and that the cellForRowAtIndexPath is doing its job (at least until it has to become displayed).
Could someone help me to sort this out?
The code:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSPredicate *predicate;
NSString *searchText = searchController.searchBar.text;
NSInteger scope = searchController.searchBar.selectedScopeButtonIndex;
if (scope == 0) {
predicate = [NSPredicate predicateWithFormat:@"SELF.drugName contains[c] %@", searchText];
}
if (scope == 1) {
predicate = [NSPredicate predicateWithFormat:@"SELF.marque contains[c] %@", searchText];
}
else if (scope == 2) {
predicate = [NSPredicate predicateWithFormat:@"SELF.classFamily contains[c] %@", searchText];
}
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[[[SubstrateStore sharedStore] allSubstrates] filteredArrayUsingPredicate:predicate]];
self.filteredResults = [NSMutableArray arrayWithArray:tempArray];
[self.tableView reloadData]; // -> I tried to remove, to change the table view, etc, it does change anything
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
Substrate *substrate = self.products[indexPath.row];
if (_searchController.active && ![_searchController.searchBar.text isEqualToString:@""]) {
substrate = self.filteredResults[indexPath.row];
}
[self configureCell:cell forProduct:substrate]; // when I check cell attributes (drugName,...), they are there...
return cell; // it works when no search is done (with the native table view) but the cell is not displayed when using the search bar (empty table view)
}
I you need more code...
Thanks!
Your problem is that you are reloading self.tableView and not the searchController's tableView.
Make sure that you also properly assign the dataSource and delegate of the searchController's tableView to self.