UISearchController

开发ios 有段时间了、 一直都处在遇到问题就自己 Search 有的时候能搜索到想要的结果、有时候发现由于关键词的原因也是找不到合适的答案。但是如果能静下来看看api 或许会发现问题其实没那么难。

昨天晚上睡觉的时候想着写点博客也算记下自己的成长过程吧!大脑中搜索目标、因为之前做过的项目中有过使用搜索的功能当时的处理方式是调转到搜索试图。想想其他app中那些搜索、也就打算先从搜索开始。直入主题 UISearchController iOS SDK 8.0 

先看看API

NS_CLASS_AVAILABLE_IOS(8_0) @interface UISearchController : UIViewController <UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>
// Pass nil if you wish to display search results in the same view that you are searching.
- (instancetype)initWithSearchResultsController:(UIViewController *)searchResultsController;

// The object responsible for updating the content of the searchResultsController.
@property (nonatomic, assign) id <UISearchResultsUpdating> searchResultsUpdater;

// Setting this property to YES is a convenience method that performs a default presentation of the search controller appropriate for how the controller is configured. Implement -presentSearchController: if the default presentation is not adequate.
@property (nonatomic, assign, getter = isActive) BOOL active;

@property (nonatomic, assign) id <UISearchControllerDelegate> delegate;
@property (nonatomic, assign) BOOL dimsBackgroundDuringPresentation;         // default is YES
@property (nonatomic, assign) BOOL hidesNavigationBarDuringPresentation;     // default is YES

@property (nonatomic, retain, readonly) UIViewController *searchResultsController;

// You are free to become the search bar's delegate to monitor for text changes and button presses.
@property (nonatomic, retain, readonly) UISearchBar *searchBar;

看上去很简单就几个属性和一个实例化方法。

- (instancetype)initWithSearchResultsController:(UIViewController *)searchResultsController; 实例化并指明结果控制器。

searchResultsUpdater; searchResultContorller 的代理,必须实现。

delegate  UISearchController的代理可以不实现。

hidesNavigationBarDuringPresentation 隐藏导航栏。

searchBar 搜索栏 。

下面看看代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
       [self layoutUI];
}

-(void)layoutUI{
    self.myTableView = [[UITableView alloc]initWithFrame:self.view.frame];
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    [self.view addSubview:self.myTableView];
    ResultViewController* resultController = [[ResultViewController alloc]init];
    self.searchController = [[UISearchController alloc]initWithSearchResultsController:resultController];
     self.searchController.searchResultsUpdater=resultController;
    [self.searchController.searchBar sizeToFit];
    self.searchController.delegate=self;
    self.searchController.hidesNavigationBarDuringPresentation=NO;
    self.searchController.hidesBottomBarWhenPushed=YES;
    self.searchController.dimsBackgroundDuringPresentation=YES;
    //self.myTableView.tableHeaderView = self.searchController.searchBar;
    [self.myTableView addSubview:self.searchController.searchBar];
}
@interface ResultViewController : UITableViewController<UISearchResultsUpdating>

@end


@interface ResultViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) NSArray* arrayData;

@end

@implementation ResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
   
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* const cellIdentifer = @"UITableViewCell";
    UITableViewCell* cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if(!cell)
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
    
    cell.textLabel.text = self.arrayData[indexPath.row];
    
    return cell;
}

如果使用init 实例化 就不能设置searchResultController 还不如直接使用 UISearchBar 。

总结:

 新 的UISearchController 简化了UISearchDisplayController 更容易学习上手。本次选择是用iOS 8 中的新特性主要在与学习。在实际项目中为了兼容低版本的sdk 目前还是使用UISearchDisplayController。

原文地址:https://www.cnblogs.com/softwaretailor/p/4686429.html