searchDisplayController在iOS 8中已弃用

Geo*_*sda 26 uisearchdisplaycontroller ios

您如何更正以下内容,以便不显示任何警告?我错过了什么?

当修正searchResultsControllersearchController它给了我一个错误"找不到对象"

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;
}
Run Code Online (Sandbox Code Playgroud)

mic*_*den 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

  • 如果Apple从Interface Builder中删除了已弃用的类,那将会很有帮助. (13认同)

小智 9

将其添加到.h文件中

<UISearchBarDelegate,UISearchResultsUpdating>

NSArray *searchResultsArray;
NSMutableArray *userMutableArray;

@property (retain, nonatomic) UISearchController *searchController;
Run Code Online (Sandbox Code Playgroud)

这到你的.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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是对原始问题的最佳答案——它展示了如何从使用已弃用的 UISearchDisplayController 转换为 UISearchController 的实用示例。 (3认同)