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

Tun*_*Dev 5 objective-c uisearchdisplaycontroller ios

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

NSDictionary *tempDictionary = nil;

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

它已被弃用并进行谷歌搜索,但我看到的只是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;
}
Run Code Online (Sandbox Code Playgroud)

CGN*_*CGN 10

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

还要感谢更新到iOS 8搜索控制器Apple的API参考的灵感

  1. 删除UISearchDisplayDelegate协议,添加UISearchResultsUpdating,也可以添加UISearchControllerDelegate
@interface YOURTableviewController : UIViewController <UITableViewDelegate, UITableViewDataSource, /*UISearchDisplayDelegate, <- Removed*/ UISearchResultsUpdating /* Added */, UISearchControllerDelegate /* Added */>
{
Run Code Online (Sandbox Code Playgroud)
  1. 添加新的UISearchController作为属性:
// New property
@property (nonatomic, strong) UISearchController *searchController;

// ...

@implementation YOURTableviewController

@synthesize searchController; // New property
Run Code Online (Sandbox Code Playgroud)

并在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];
}
Run Code Online (Sandbox Code Playgroud)
  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
{
    //..
}
Run Code Online (Sandbox Code Playgroud)
  1. 替换并删除过时的UISearchDisplayDelegate方法
#pragma mark -
#pragma mark UISearchDisplayDelegate

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ ... }
Run Code Online (Sandbox Code Playgroud)
  1. 用'searchController'替换'searchDisplayController'到处

  2. 进行更换

代码如:

if (tableView == self.searchDisplayController.searchResultsTableView)
Run Code Online (Sandbox Code Playgroud)

可以替换为

if (self.searchController.isActive)
Run Code Online (Sandbox Code Playgroud)

代码如:

[self.searchDisplayController setActive:YES];
Run Code Online (Sandbox Code Playgroud)

可以替换为

[self.searchController setActive:YES];
Run Code Online (Sandbox Code Playgroud)


Siv*_*ina 1

由于已弃用,您需要根据您的目的UISearchDisplayController使用。UISearchController点击此链接使用 UISearchController

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