UI基础 - UISearchController

■ 简言

1. UISearchController 是 iOS 8 之后推出的一个新的控件, 用于创建搜索条及管理搜索事件,一般和 UITableView 结合使用,很少会单独使用它

 1 // 初始化方法, 参数是展示搜索结果的控制器,如果是在当前控制器展示搜索结果, 就传 nil
 2 - (instancetype)initWithSearchResultsController:(nullable UIViewController *)searchResultsController;
 3 
 4 // 负责更新搜索结果的代理, 需要遵循 UISearchResultsUpdating 协议
 5 @property (nullable, nonatomic, weak) id <UISearchResultsUpdating> searchResultsUpdater;
 6 
 7 // 搜索控制器是否是活跃状态, 当在一个控制器展示搜索结果的时候, 可以此来判断返回的数据源
 8 @property (nonatomic, assign, getter = isActive) BOOL active;
 9 // 控制器代理  遵循 UISearchControllerDelegate协议
10 @property (nullable, nonatomic, weak) id <UISearchControllerDelegate> delegate;
11 // 当搜索框激活时, 是否添加一个透明视图
12 @property (nonatomic, assign) BOOL dimsBackgroundDuringPresentation __TVOS_PROHIBITED; 
13 @property (nonatomic, assign) BOOL obscuresBackgroundDuringPresentation NS_AVAILABLE_IOS(9_1); // default is YES
14 // 当搜索框激活时, 是否隐藏导航条
15 @property (nonatomic, assign) BOOL hidesNavigationBarDuringPresentation;     // default is YES
16 
17 @property (nullable, nonatomic, strong, readonly) UIViewController *searchResultsController;
18 @property (nonatomic, strong, readonly) UISearchBar *searchBar;

■ 使用方式

  1 #import "ViewController.h"
  2 @interface ViewController ()<UISearchResultsUpdating,UITableViewDataSource,UITableViewDelegate>
  3 @property(nonatomic,strong)NSArray *dataArray;
  4 @property(nonatomic,strong)NSArray *searchArray;
  5 @property(nonatomic,strong)UISearchController *searchVC;
  6 @property(nonatomic,strong)UITableView *tableView;
  7 @end
  8 
  9 @implementation ViewController
 10 
 11 - (void)viewDidLoad {
 12     [super viewDidLoad];
 13     
 14     // 标题
 15     self.navigationItem.title = @"UISearchController";
 16     self.navigationController.navigationBar.translucent = NO;
 17     // UITableView
 18     self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
 19     self.tableView.delegate = self;
 20     self.tableView.dataSource = self;
 21     [self.view addSubview:self.tableView];
 22     
 23     // UISearchController
 24     // 参数是展示搜索结果的控制器, 如果是在当前控制器展示搜索结果, 就传nil
 25     self.searchVC = [[UISearchController alloc] initWithSearchResultsController:nil];
 26     self.searchVC.searchBar.barTintColor = [UIColor cyanColor]; // 状态条颜色
 27     self.searchVC.searchBar.tintColor = [UIColor purpleColor];  // 渲染颜色:光标、取消按钮
 28     self.searchVC.searchBar.prompt = @"搜索栏";
 29     self.searchVC.searchBar.placeholder = @"搜索";
 30     self.searchVC.hidesNavigationBarDuringPresentation = NO; // 开始搜索时是否推掉 NavigationBar
 31     self.searchVC.searchResultsUpdater = self; // 代理
 32     self.searchVC.dimsBackgroundDuringPresentation = NO; // 因为在当前控制器展示结果,所以不需要这个透明视图
 33     [self.searchVC.searchBar sizeToFit]; // 自适应
 34     self.tableView.tableHeaderView = self.searchVC.searchBar;
 35 }
 36 
 37 #pragma mark - load_on_demand
 38 - (NSArray*)dataArray{
 39     
 40     if (!_dataArray) {
 41         
 42         NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
 43         // 制造 100 条数据
 44         for (int i=0; i<100; i++) {
 45             
 46             int j=arc4random() %(122-65+1)+65;
 47             int k=arc4random() %(122-65+1)+65;
 48             int t=arc4random() %(122-65+1)+65;
 49             int l=arc4random() %(122-65+1)+65;
 50             int m=arc4random() %(122-65+1)+65;
 51             int n=arc4random() %(122-65+1)+65;
 52             
 53             NSString *str=[NSString stringWithFormat:@"%c%c%c%c%c%c%d",j,k,l,m,n,t,i];
 54             
 55             [array addObject:str];
 56             
 57         }
 58         self.dataArray = [NSArray arrayWithArray:array];
 59     }
 60     
 61     return _dataArray;
 62 }
 63 
 64 #pragma mark - <UITableViewDataSource>
 65 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 66     
 67     return 1;
 68 }
 69 
 70 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 71     return self.searchArray.count;
 72 }
 73 
 74 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 75     
 76     static NSString *cellIndentifier = @"cell";
 77     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier ];
 78     if (!cell) {
 79         cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
 80     }
 81     cell.textLabel.text = self.searchArray[indexPath.row];
 82     return cell;
 83 }
 84 
 85 #pragma mark - <UITableViewDelegate>
 86 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 87     
 88     self.searchVC.searchBar.text = [self.searchArray objectAtIndex:indexPath.row];;
 89     [self.searchVC.searchBar resignFirstResponder];// 键盘回收
 90 }
 91 
 92 #pragma mark - <UISearchResultsUpdating>
 93 // 动态检测:输入内容时触发
 94 - (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
 95     
 96     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[c]%@",self.searchVC.searchBar.text];// 模糊查询
 97     self.searchArray = [NSArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:predicate]];
 98     [self.tableView reloadData];
 99     
100     // 检索内容为空,则展示全部数据
101     if (_searchVC.searchBar.text.length==0) {
102         
103         self.searchArray = self.dataArray;
104         [self.tableView reloadData];
105     }
106 }
107 
108 @end

运行效果:输入搜索内容

  

posted on 2018-04-10 11:28  低头捡石頭  阅读(154)  评论(0编辑  收藏  举报

导航