searchDisplayController deprecated in iOS 8
Solution 1
The
UISearchController
class replaces theUISearchDisplayController
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:
Solution 2
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;
}
Solution 3
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.
George Asda
Updated on May 08, 2020Comments
-
George Asda about 4 years
How do you correct the following so no warnings appear? What am I missing?
When correcting the
searchResultsController
tosearchController
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; }