1

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]];
}
AnthoPak
  • 4,191
  • 3
  • 23
  • 41
AD Tee
  • 345
  • 4
  • 21
  • Your question is unclear... What does `searchForText` do ? What happens when you select a search result ? – AnthoPak Jun 28 '18 at 10:44
  • Hi AnthoPak, updated with searchForText function. When I tried to click on search result, cancel button in UISearchController will disappear and the list will reset back to default listing. – AD Tee Jun 28 '18 at 12:29
  • Hum... Maybe you can try to add `self.searchController.obscuresBackgroundDuringPresentation = NO;` – AnthoPak Jun 28 '18 at 12:35
  • Hi AnthoPak, thanks alot, its works!!! but the UISearchController bar will remain after I click on search result (Expected result: after click on selected result will push to another ViewController page). How can I hide it after click in didSelectRowAtIndexPath? – AD Tee Jun 28 '18 at 13:26
  • I found this post and it help me to solve the issue as mentioned above. https://stackoverflow.com/questions/35093042/uisearchcontroller-search-bar-position-drops-64-points – AD Tee Jun 28 '18 at 14:06
  • Sorry i've just seen your comments ! Glad to know you've solve your issue. Will add this as an answer as it worked for you :) – AnthoPak Jun 28 '18 at 14:31

1 Answers1

1

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.

AnthoPak
  • 4,191
  • 3
  • 23
  • 41