如何纠正以下错误,使警告不会出现?我遗漏了什么?
当将searchResultsController更正为searchController时,它给我一个错误"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;
}发布于 2014-09-14 02:34:48
UISearchController类取代了UISearchDisplayController类,用于管理与搜索相关的界面的显示。
来源:https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html
所以,正如rmaddy所说的,如果你想摆脱过时的警告,那就停止使用过时的类。请改用UISearchController:
发布于 2016-03-17 13:23:21
将此代码添加到.h文件中
<UISearchBarDelegate,UISearchResultsUpdating>
NSArray *searchResultsArray;
NSMutableArray *userMutableArray;
@property (retain, nonatomic) UISearchController *searchController;并将其添加到您的.m文件中
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;
}发布于 2014-12-26 06:58:19
我希望从UISearchDisplayController迁移到UISearchController。所以,我在GitHub上找到了这个:https://github.com/dempseyatgithub/Sample-UISearchController
下载/克隆它,您将能够看到如何在UITableView和UICollectionView中使用UISearchController。
它拥有从UISearchDisplayController升级到UISearchController所需的一切。
UISearchController文档也非常有用。作为入门指南,请查看Sample-UISearchController/MasterViewController_TableResults.m和Sample-UISearchController/MasterViewController_FilterResults.m
如果你还需要支持iOS 7(如果你真的要把你的应用部署到应用商店,我个人推荐),那么这样做:
if([UISearchController class]){
//Create an UISearchController and add it to your UITableViewController
}else{
//Create an UISearchDisplayController and add it to your UITableViewController
}注意:如果你想同时支持两个版本的iOS,你必须通过编程来完成所有的工作。
https://stackoverflow.com/questions/25826332
复制相似问题