Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searchDisplayController deprecated in iOS 8

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; } 
like image 756
George Asda Avatar asked Sep 13 '14 18:09

George Asda


2 Answers

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

like image 117
michaelsnowden Avatar answered Sep 22 '22 19:09

michaelsnowden


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; } 
like image 27
RStack Avatar answered Sep 22 '22 19:09

RStack