【问题标题】:search Display Controller is Deprecated in iOS8搜索显示控制器在 iOS8 中已弃用
【发布时间】:2017-09-20 20:31:29
【问题描述】:

我在尝试运行我的代码时收到上述警告消息。

NSDictionary *tempDictionary = nil;

    if (self.tableView == self.searchDisplayController.searchResultsTableView) {
        tempDictionary = [filteredCompanyArray objectAtIndex:indexPath.row];
    }
    else {
        tempDictionary= [self.client_list objectAtIndex:indexPath.row];
    }

它已被弃用并进行了谷歌搜索,但我看到的只是 Swift 教程。

我在 http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view 上学习了 Ray Wenderlich 教程,但现在我被卡住了。

#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    // Update the filtered array based on the search text and scope.
    // Remove all objects from the filtered search array
    [self.filteredCompanyArray removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
    filteredCompanyArray = [NSMutableArray arrayWithArray:[self.client_list filteredArrayUsingPredicate:predicate]];
}

#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    // Tells the table data source to reload when text changes
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    // Tells the table data source to reload when scope bar selection changes
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

【问题讨论】:

标签: ios objective-c uisearchdisplaycontroller


【解决方案1】:

下面的食谱对我有用。我刚刚成功更新了之前 iOS 7 代码的多个场景。

也感谢Updating to the iOS 8 Search ControllerApple's API Reference的启发

  1. 删除 UISearchDisplayDelegate 协议,添加 UISearchResultsUpdating 和 UISearchControllerDelegate 代替
@interface YOURTableviewController : UIViewController <UITableViewDelegate, UITableViewDataSource, /*UISearchDisplayDelegate, <- Removed*/ UISearchResultsUpdating /* Added */, UISearchControllerDelegate /* Added */>
{
  1. 添加新的 UISearchController 作为属性:
// New property
@property (nonatomic, strong) UISearchController *searchController;

// ...

@implementation YOURTableviewController

@synthesize searchController; // New property

并在 didLoad 方法中初始化新属性

- (void)viewDidLoad
{
     // New code start -
     self.searchController = [[UISearchController alloc]     initWithSearchResultsController:nil];
     self.searchController.searchResultsUpdater = self;
     self.searchController.delegate = self;
     self.searchController.dimsBackgroundDuringPresentation = NO;

     self.searchController.searchBar.delegate = self;

     self.searchController.searchBar.barTintColor = [UIColor orangeColor];
     [self.tableview setTableHeaderView:self.searchController.searchBar];
     self.definesPresentationContext = YES;
     // - New code end

     // Previous code
     //self.searchDisplayController.searchBar.barTintColor = [UIColor orange]; 

     [super viewDidLoad];
}
  1. 添加新的 UISearchResultsUpdating 方法,更新 UISearchBarDelegate 方法,并可能添加如下帮助的额外方法,可能还有 UISearchControllerDelegate 方法
#pragma mark -
#pragma mark UISearchResultsUpdating

- (void)updateSearchResultsForSearchController:(UISearchController *)_searchController
{
    NSString *searchString = _searchController.searchBar.text;
    [self searchForText:searchString];
    [self.tableview reloadData];
}

#pragma mark -
#pragma mark UISearchBarDelegate

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self updateSearchResultsForSearchController:self.searchController];
}

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    [self updateSearchResultsForSearchController:self.searchController];
}

// Extra method
- (void)searchForText:(NSString *)searchString
{
    /* Put here everything that is in the method:
     - (BOOL)searchDisplayController:(UISearchDisplayController *)controller     shouldReloadTableForSearchString:(NSString *)searchString
     ...BUT WITH NO RETURN VALUE */
}

#pragma mark -
#pragma mark UISearchControllerDelegate

- (void)willPresentSearchController:(UISearchController *)searchController
{
    //..
}
- (void)didPresentSearchController:(UISearchController *)searchController
{
    //..
}
- (void)willDismissSearchController:(UISearchController *)searchController
{
    //..
}
- (void)didDismissSearchController:(UISearchController *)searchController
{
    //..
}
  1. 替换和删除过时的 UISearchDisplayDelegate 方法
#pragma mark -
#pragma mark UISearchDisplayDelegate

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ ... }
  1. 用“searchController”替换任何地方的“searchDisplayController”

  2. 进行替换

代码如下:

if (tableView == self.searchDisplayController.searchResultsTableView)

可以替换为

if (self.searchController.isActive)

代码如下:

[self.searchDisplayController setActive:YES];

可以替换为

[self.searchController setActive:YES];

【讨论】:

    【解决方案2】:

    是的 UISearchDisplay Controller 已弃用,您应该改用 UISearchController

    首先创建搜索控制器

    @property (strong, nonatomic) UISearchController *searchController;
    

    初始化搜索控制器属性

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    

    将搜索控制器与您的表格视图绑定

    self.tableView.tableHeaderView = self.searchController.searchBar;
    

    实现 UISearchResultUpdating 委托

    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
    {
     NSString *searchString = searchController.searchBar.text;
    [self searchForText:searchString 
     scope:searchController.searchBar.selectedScopeButtonIndex];
    [self.tableView reloadData];
    }
    

    你也可以使用你的过滤方法来过滤结果

    您的搜索逻辑将放在此处

    -(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
    

    您可以找到更多详细信息

    http://useyourloaf.com/blog/updating-to-the-ios-8-search-controller/

    【讨论】:

      【解决方案3】:

      由于UISearchDisplayController 已被弃用,您需要使用UISearchController 来满足您的目的。关注此链接Using UISearchController

      另一个可能对你有用的链接UISearchController Vs UISearchDisplayController

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-18
        • 2016-04-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多