问题
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;
}
回答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:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchController/index.html#//apple_ref/occ/cl/UISearchController
回答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;
}
回答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.
回答4:
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.
来源:https://stackoverflow.com/questions/25826332/searchdisplaycontroller-deprecated-in-ios-8