Search code examples
iosobjective-cuitableviewuisearchcontroller

SearchController in iOS 11. Can't touch in any tableView cell


System behavior of the search controller:

  • Go to iPhone settings
  • Slide to show searchBar
  • Tap on searchBar
  • TableView has gray color now and if I touch anywhere (not in searchBar) searchBar will hide
  • I enter any text and tableView has normal color now, I can click into any of searched cells.

In my application, after tapped any text tableView is still gray. I can't tap in any of searched cells. The difference is also that when I searching I update old tableView (I don't have different tableView after searching) by using updateSearchResultsForSearchController and reloadData after that.

The question is how to hide this gray view and give chance to tap on cells after enter any text in searchcontroller?

The way I create search controller:

UISearchController * search = [[UISearchController alloc] initWithSearchResultsController:nil];
search.searchResultsUpdater = self;
self.navigationItem.searchController = search;
self.navigationItem.hidesSearchBarWhenScrolling = YES;
self.navigationItem.searchController.searchBar.barTintColor = [UIColor blueColor];
self.navigationItem.searchController.searchBar.delegate = self;

and how I update tableView cells:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    self.actualSearchController = searchController;
    self.filteredData = [self filterContentForSearchText:searchController.searchBar.text];
    [self.tableView reloadData];
}

- (NSMutableArray<MySectionContainter *> *)filterContentForSearchText:(NSString *)text {
    NSMutableArray<MySectionContainter *> *sections = [NSMutableArray array];

    for (MySectionContainter *section in self.tableData) {
        NSArray *objects = [sectionInfo.rowObjects filteredArrayUsingPredicate:[self.filterSource predicateForSearching:text]];

        if ([objects count] > 0) {
            [sections addObject:[[MySectionContainter alloc] initWithTitle:section.title andObjects:objects]];
        }
    }

    return sections;
}

- (NSPredicate *)predicateForSearching:(NSString *)text {
    return [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        if ([evaluatedObject isKindOfClass:[MyClass class]]) {
            return [((MyClass *)evaluatedObject).title containsString:text];
        }


    return NO;
    }];
}

Example project code: https://pastebin.com/m9V2dunB

Screenshot:

enter image description here

System behavior:

enter image description here

Test project: https://www.sendspace.com/file/2lhedj


Solution

  • I solve problem in your demo project by adding this line to viewDidLoad

    search.dimsBackgroundDuringPresentation = NO;

    Just remove dim background of UISearchController, everything will work fine.