搜索显示控制器在iOS8中已弃用

时间:2015-07-11 12:58:57

标签: ios objective-c uisearchdisplaycontroller

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

NSDictionary *tempDictionary = nil;

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

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

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

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

3 个答案:

答案 0 :(得分:9)

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

还要感谢Updating to the iOS 8 Search ControllerApple's API Reference

的灵感
  1. 删除UISearchDisplayDelegate协议,添加UISearchResultsUpdating,或者改为使用UISearchControllerDelegate
  2. @interface YOURTableviewController : UIViewController <UITableViewDelegate, UITableViewDataSource, /*UISearchDisplayDelegate, <- Removed*/ UISearchResultsUpdating /* Added */, UISearchControllerDelegate /* Added */>
    {
    
    1. 将新的UISearchController添加为属性:
    2. // 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方法
      2. #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方法
        2. #pragma mark -
          #pragma mark UISearchDisplayDelegate
          
          - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
          { ... }
          
          1. 替换&#39; searchDisplayController&#39;随处可见&#39; searchController&#39;

          2. 进行替换

          3. 代码如:

            if (tableView == self.searchDisplayController.searchResultsTableView)
            

            可以替换为

            if (self.searchController.isActive)
            

            代码如:

            [self.searchDisplayController setActive:YES];
            

            可以替换为

            [self.searchController setActive:YES];
            

答案 1 :(得分:1)

由于UISearchDisplayController已弃用,您需要使用UISearchController来达到目的。请点击此链接Using UISearchController

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

答案 2 :(得分:0)

是的UISearchDisplay控制器已弃用,您应该使用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/

相关问题