Search code examples
iosobjective-cuisearchcontroller

How to add the search option for a tableview in ios 9 using uisearchcontroller, with objectiveC


I have a tableView that displays data successfully and now what I want is to provide the search function for it. UISearchDisplayController is deprecated in iOS 9 and I'm new to iOS. So please tell me the way to do this. If anyone can provide a code step by step,I appreciate it and it will also help others.This is my tableView code.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [airportList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ci"];

    Details *newDetails = [airportList objectAtIndex:indexPath.row];

    cell.textLabel.text = newDetails.airport;

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    Details *newDetails = [airportList objectAtIndex:indexPath.row];
    NSString *selectedText = newDetails.airport;
    [[NSUserDefaults standardUserDefaults] setObject:selectedText forKey:@"st"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [self dismissViewControllerAnimated:YES completion:nil];
}

Solution

  • You can use UISearchController in iOS 9

    First declare a property for UISearchController

    @property (strong, nonatomic) UISearchController *searchController;
    

    then, in viewDidLoad

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    

    When creating the UISearchController we do not need a separate search results controller as we will use the UITableViewController itself. Likewise we will also use the UITableViewController to update the search results by having it implement the UISearchResultsUpdating protocol. We do not want to dim the underlying content as we want to show the filtered results as the user types into the search bar. The UISearchController takes care of creating the search bar for us. The UITableViewController will also act as the search bar delegate for when the user changes the search scope.

    Next, add searchBar to tableview header

    self.tableView.tableHeaderView = self.searchController.searchBar;
    

    Since the search view covers the table view when active we make the UITableViewController define the presentation context:

    self.definesPresentationContext = YES;
    

    We need to implement the UISearchResultsUpdating delegate to generate the new filtered results anytime the search text changes:

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