首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 8中不推荐使用searchDisplayController

iOS 8中不推荐使用searchDisplayController
EN

Stack Overflow用户
提问于 2014-09-14 02:29:33
回答 4查看 46.9K关注 0票数 28

如何纠正以下错误,使警告不会出现?我遗漏了什么?

当将searchResultsController更正为searchController时,它给我一个错误"object not found“

代码语言:javascript
复制
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;
}
EN

回答 4

Stack Overflow用户

发布于 2014-09-14 02:34:48

UISearchController类取代了UISearchDisplayController类,用于管理与搜索相关的界面的显示。

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

所以,正如rmaddy所说的,如果你想摆脱过时的警告,那就停止使用过时的类。请改用UISearchController

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

票数 12
EN

Stack Overflow用户

发布于 2016-03-17 13:23:21

将此代码添加到.h文件中

代码语言:javascript
复制
<UISearchBarDelegate,UISearchResultsUpdating>

NSArray *searchResultsArray;
NSMutableArray *userMutableArray;

@property (retain, nonatomic) UISearchController *searchController;

并将其添加到您的.m文件中

代码语言:javascript
复制
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;
}
票数 12
EN

Stack Overflow用户

发布于 2014-12-26 06:58:19

我希望从UISearchDisplayController迁移到UISearchController。所以,我在GitHub上找到了这个:https://github.com/dempseyatgithub/Sample-UISearchController

下载/克隆它,您将能够看到如何在UITableViewUICollectionView中使用UISearchController

它拥有从UISearchDisplayController升级到UISearchController所需的一切。

UISearchController文档也非常有用。作为入门指南,请查看Sample-UISearchController/MasterViewController_TableResults.m和Sample-UISearchController/MasterViewController_FilterResults.m

如果你还需要支持iOS 7(如果你真的要把你的应用部署到应用商店,我个人推荐),那么这样做:

代码语言:javascript
复制
if([UISearchController class]){
//Create an UISearchController and add it to your UITableViewController
}else{
//Create an UISearchDisplayController and add it to your UITableViewController 
}

注意:如果你想同时支持两个版本的iOS,你必须通过编程来完成所有的工作。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25826332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档