Search code examples
tableviewuisearchbaruisearchdisplaycontrolleruirefreshcontroltableheader

Using UIRefreshControl with UISearchBar search results


I'm trying to get a behavior like the Mail app where there is a UISearchBar and you can pull to refresh the search results. Currently, I have the Search Display Controller with the UISearchBar connected in IB and everything works fine (I can refresh when in the main tableview and I can search).

But when I search and try to pull to refresh the search results (like the mail app), there is no UIRefreshControl. Check the video below:

Screen Recording

I guess I have to make the UISearchBar the headerView of the tableView but I can't reference the UISearchBar since it's an outlet of the Search Display Controller in IB. Here is my code for the UIRefreshControl :

    //Pull to Refresh
    refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(pullToRefresh)      
    forControlEvents:UIControlEventValueChanged];


    [self.carTableView addSubview:refreshControl];

I'm using the above code in the -viewDidLoad method of the UIViewController that contains the tableView


Solution

  • I solved the problem by creating another UIRefreshControl and adding it to the UISearchDispayController every time the user type a search text in the search bar which I can detect in - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView . Here is the code:

    //in viewDidLoad
    //Pull to refresh in the search
        searchRefreshControl = [[UIRefreshControl alloc] init];
        [searchRefreshControl addTarget:self action:@selector(pullToRefresh) forControlEvents:UIControlEventValueChanged];
    

    Then:

    //in - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
    
    //add the refresh control to the search display controller tableview
        [self.searchDisplayController.searchResultsTableView addSubview:searchRefreshControl];