27

How do you correct the following so no warnings appear? What am I missing?

When correcting the searchResultsController to searchController it gives me an error "object not found"

if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [_content objectAtIndex:indexPath.row];
    }

    return cell;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
  shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                       objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}
Pang
  • 8,605
  • 144
  • 77
  • 113
George Asda
  • 2,029
  • 2
  • 27
  • 52

4 Answers4

12

The UISearchController class replaces the UISearchDisplayController class for managing the display of search-related interfaces.

Source : https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

So, as rmaddy said, if you want to get rid of the deprecated warnings, stop using the deprecated classes. Use UISearchController instead:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchController/index.html#//apple_ref/occ/cl/UISearchController

Pang
  • 8,605
  • 144
  • 77
  • 113
michaelsnowden
  • 5,532
  • 2
  • 32
  • 72
11

Add this to your .h file

<UISearchBarDelegate,UISearchResultsUpdating>

NSArray *searchResultsArray;
NSMutableArray *userMutableArray;

@property (retain, nonatomic) UISearchController *searchController;

and this to your .m file

userMutableArray = [[NSMutableArray alloc] initWithObjects:@"Jack",@"Julie", nil];
searchResultsArray = [[NSArray alloc]init];

self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchBar.scopeButtonTitles = [[NSArray alloc]initWithObjects:@"UserId", @"JobTitleName", nil];
self.searchController.searchBar.delegate = self;
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
self.tableView.tableHeaderView = self.searchController.searchBar;


-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString *searchString = self.searchController.searchBar.text;
    NSPredicate *resultPredicate;
    NSInteger scope = self.searchController.searchBar.selectedScopeButtonIndex;
    if(scope == 0){
        resultPredicate = [NSPredicate predicateWithFormat:@"userId contains[c] %@",searchString];
    }else{
        resultPredicate = [NSPredicate predicateWithFormat:@"jobTitleName contains[c] %@",searchString];
    }
    searchResultsArray = [userMutableArray filteredArrayUsingPredicate:resultPredicate];
    [self.tableView reloadData];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(self.searchController.active){
        return [searchResultsArray count];
    }else{
        return [userMutableArray count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    if(self.searchController.active){
        cell.textLabel.text = [searchResultsArray objectAtIndex:indexPath.row];
    }else{
        cell.textLabel.text = [userMutableArray objectAtIndex:indexPath.row];
    }
    return cell;
}
fishinear
  • 5,674
  • 3
  • 32
  • 80
RStack
  • 220
  • 4
  • 15
  • 3
    This is the best answer to the original question – it shows a practical and working example of how to convert from using the deprecated UISearchDisplayController to UISearchController. – mbbeme Apr 02 '17 at 21:29
  • simple and detail answer. – Mihir Oza May 08 '17 at 07:32
4

I was looking to migrate from UISearchDisplayController to UISearchController. So, I found this on GitHub: https://github.com/dempseyatgithub/Sample-UISearchController
Download/clone it and you'll be able to see how to use UISearchController with UITableView and UICollectionView.
It has everything you need to upgrade from UISearchDisplayController to UISearchController.
The UISearchController documentation is also really helpful. For getting started, look at the Sample-UISearchController/MasterViewController_TableResults.m and Sample-UISearchController/MasterViewController_FilterResults.m
If you also need to support iOS 7(something I personally recommend if you are really about to deploy your app to the App Store) do this:

if([UISearchController class]){
//Create an UISearchController and add it to your UITableViewController
}else{
//Create an UISearchDisplayController and add it to your UITableViewController 
}

Note: You'll have to do everything programatically if you want to support both versions of iOS.

Mihir Oza
  • 2,633
  • 3
  • 30
  • 56
Gabriel Tomitsuka
  • 1,061
  • 10
  • 24
0

We've been working for a long time to get the new UISearchController to rotate properly.

Here's what it looked like before:

After much time we essentially "gave up" on making the rotation work properly. Instead we simply hide the search controller before the rotation happens. The user then has to tap on the search button on the rotated view to bring up the search bar again.

Here’s the relevant code:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [self.searchController dismissViewControllerAnimated:YES completion:nil];
    self.searchControllerWasActive = NO;
    self.searchButton.enabled = YES;
}

Important note: Our code uses a UIViewController and not UITableViewController. The screen requires additional buttons which is why we cannot use UITableViewController. Using UISearchController on a UITableViewController does not exhibit the rotation issues.

We see this as a necessary work around given the current state of UISearchController. It would be much better to have a real solution to this problem.

George Asda
  • 2,029
  • 2
  • 27
  • 52