Search code examples
iosobjective-cuisearchcontroller

Unable to select search result in UISearchController


I have a tableView list in my IOS app. I wish to search my tableView list with UISearchController. By default the list will display correctly.

When I active search controller (Click on the UISearchController), my existing list will disappear and when I key in related keywords, result will be displayed accordingly.

But when I check on related search result, searchController will be deactivate and back to default list.

Any idea?

- (void)viewDidLoad {
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.delegate = self;

    self.searchController.searchBar.delegate = self;

    self.searchController.searchBar.barTintColor = ThemeLightGrayColor;

    self.searchController.hidesNavigationBarDuringPresentation = NO;

    [self.searchController.searchBar.layer setBorderColor:[UIColor colorWithRed:229.0/255 green:229.0/255 blue:229.0/255 alpha:1].CGColor];
    [self.searchController.searchBar setPlaceholder:@"Search"];
    [self.searchController.searchBar.layer setBorderWidth:0.5];
    [self.searchController.searchBar setKeyboardType:UIKeyboardTypeDefault];
    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"Bitter" size:14]}];

    [self.searchController.searchBar sizeToFit];


    _searchResultArr=[NSMutableArray array];
    _tableView.tableHeaderView = ({
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, DCNaviH, 0, self.searchController.searchBar.frame.size.height + 50);
        _segPromotion = [[UISegmentedControl alloc] initWithItems:@[@"All",@"Near Me",@"Coming Soon"]];
        _segPromotion.selectedSegmentIndex = 0;
        _segPromotion.backgroundColor = ThemeWhiteColor;
        _segPromotion.frame = CGRectMake(10, self.searchController.searchBar.frame.origin.y+5 + self.searchController.searchBar.frame.size.height , ScreenW-20, 30);
        [_segPromotion setTitleTextAttributes:@{NSFontAttributeName :  [UIFont fontWithName:@"Bitter" size:13],NSForegroundColorAttributeName: ThemeDarkBlueColor } forState:UIControlStateNormal];
        [_segPromotion setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"Bitter" size:13],NSForegroundColorAttributeName : ThemeWhiteColor} forState:UIControlStateSelected];
        [_segPromotion addTarget:self action:@selector(SegmentChangeViewValueChanged:) forControlEvents:UIControlEventValueChanged];

        [view addSubview:self.searchController.searchBar];
        [view addSubview:_segPromotion];
        view;
    });
    [self.view addSubview:_tableView];
}


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = searchController.searchBar.text;
    [self searchForText:searchString];
    [self.tableView reloadData];
}



- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self updateSearchResultsForSearchController:self.searchController];
}

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    [self updateSearchResultsForSearchController:self.searchController];
}


- (void)searchForText:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[self.searchBar scopeButtonTitles][self.searchBar.selectedScopeButtonIndex]];
}

Solution

  • What you have to do is adding self.searchController.obscuresBackgroundDuringPresentation = NO; when initializing the searchController.

    This is because, as you have used [[UISearchController alloc] initWithSearchResultsController:nil], the results will be shown in the same view controller (because of nil parameter). For this reason, if obscuresBackgroundDuringPresentation is set to YES (this is the case by default), the results area won't be touchable.