searchDisplayController在iOS 8中已弃用

时间:2014-09-13 18:29:33

标签: ios uisearchdisplaycontroller

如何更正以下内容,以免出现警告? 我错过了什么?

searchResultsController更改为searchController时,会出现错误“找不到对象”

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;
}

4 个答案:

答案 0 :(得分:12)

  

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

答案 1 :(得分:9)

将此添加到您的.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;
}

答案 2 :(得分:4)

我希望从UISearchDisplayController迁移到UISearchController。所以,我在GitHub上找到了这个:https://github.com/dempseyatgithub/Sample-UISearchController
下载/克隆它,你将能够看到如何将UISearchControllerUITableViewUICollectionView一起使用。
它包含从UISearchDisplayController升级到UISearchController所需的一切 UISearchController文档也非常有用。要开始使用,请查看Sample- UISearchController / MasterViewController_TableResults.m和Sample-UISearchController / MasterViewController_FilterResults.m
如果您还需要支持iOS 7(我个人推荐的内容,如果您真的要将应用程序部署到App Store),请执行以下操作:

if([UISearchController class]){
//Create an UISearchController and add it to your UITableViewController
}else{
//Create an UISearchDisplayController and add it to your UITableViewController 
}

注意:如果您想支持两个版本的iOS,则必须以编程方式执行所有操作。

答案 3 :(得分:0)

我们已经工作了很长时间才能让新的UISearchController正常旋转。

以下是它的样子:

经过很长时间,我们基本上放弃了#34;使旋转正常工作。相反,我们只是在旋转发生之前隐藏搜索控制器。然后,用户必须点击旋转视图上的搜索按钮再次调出搜索栏。

以下是相关代码:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [self.searchController dismissViewControllerAnimated:YES completion:nil];
    self.searchControllerWasActive = NO;
    self.searchButton.enabled = YES;
}

重要说明:我们的代码使用UIViewController而不是UITableViewController。屏幕需要额外的按钮,这就是为什么我们不能使用UITableViewController。在UITableViewController上使用UISearchController不会出现旋转问题。

鉴于UISearchController的当前状态,我们认为这是必要的工作。对这个问题有一个真正的解决方案会好得多。

相关问题