UISearchResultsUpdating

UISearchResultsUpdating

尝试运行我的代码时,出现上述警告消息。

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

最佳答案

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

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

  • 删除UISearchDisplayDelegate协议(protocol),添加UISearchResultsUpdating并可能添加UISearchControllerDelegate

  • @interface YOURTableviewController : UIViewController <UITableViewDelegate, UITableViewDataSource, /*UISearchDisplayDelegate, <- Removed*/ UISearchResultsUpdating /* Added */, UISearchControllerDelegate /* Added */>
    {
    
  • 添加新的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];
    }
    
  • 添加新的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
    {
        //..
    }
    
  • 替换和删除过时的UISearchDisplayDelegate方法

  • #pragma mark -
    #pragma mark UISearchDisplayDelegate
    
    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
    { ... }
    
  • 到处都用'searchController'替换'searchDisplayController'
  • 进行替换

  • 像这样的代码:

    if (tableView == self.searchDisplayController.searchResultsTableView)
    

    可以替换为

    if (self.searchController.isActive)
    

    像这样的代码:

    [self.searchDisplayController setActive:YES];
    

    可以替换为

    [self.searchController setActive:YES];
    

    10-08 17:41