• UISearchDisplayController简单使用


    最近在做一个简单的app入门,中间有一个页面用到了搜索框,本来以为很简单的控件,没想到用到的时候才发现很麻烦。

    搜索框使用过程大约有以下几个状态:不活跃-活跃-输入关键词-根据关键词动态列出相关结果-选中搜索结果或取消搜索

    // ViewController.h
    #import
    <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
    //相关协议
    @property (strong, nonatomic) NSMutableArray *dataList;//搜索数据源 @property (strong, nonatomic) NSMutableArray *searchList;//搜索结果 @property (strong, nonatomic) UITableView *tableView;//数据源tableview @end
    // ViewController.m
    #import
    "ViewController.h" @interface ViewController () @property (nonatomic) UISearchDisplayController *searchDisplayController; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.dataList = [NSMutableArray arrayWithCapacity:10]; self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 104, self.view.bounds.size.width, self.view.bounds.size.height - 40)]; for (NSInteger i = 0; i < 10; i++) { [self.dataList addObject:[NSString stringWithFormat:@"%ld-yang",i]]; } self.tableView.delegate = self; self.tableView.dataSource = self; UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 40)]; [searchBar setPlaceholder:@"关键字搜索"]; searchBar.delegate = self; [self.view addSubview:searchBar]; self.searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self]; _searchDisplayController.delegate = self; [_searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; _searchDisplayController.searchResultsTableView.dataSource = self; _searchDisplayController.searchResultsTableView.delegate = self; [self.view addSubview:self.tableView]; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      
    if (tableView == self.searchDisplayController.searchResultsTableView) { return [self.searchList count]; }else{ return [self.dataList count]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; }
    if (tableView == self.tableView){//判断当前tableview是否为搜索结果 [cell.textLabel setText:self.dataList[indexPath.row]]; }else{ [cell.textLabel setText:self.searchList[indexPath.row]]; } return cell; }
    //协议方法:开始搜索
    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { NSLog(@"search begin"); searchBar.frame = CGRectMake(0, 20, 320, 44); [self.searchDisplayController setActive:YES]; return YES; }
    //协议方法:搜索结束
    - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { NSLog(@"search end"); searchBar.frame = CGRectMake(0, 64, 320, 44); [self.searchDisplayController setActive:NO]; return YES; }
    //关键词变化时实时更新搜索结果
    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@",searchString];   

      //清空结果数组 [self.searchList removeAllObjects];
    //过滤数据 NSArray *a = [_dataList filteredArrayUsingPredicate:preicate]; self.searchList = [NSMutableArray arrayWithArray:a]; //刷新表格 return YES; } @end
  • 相关阅读:
    在一天的24小时之中,时钟的时针、分针和秒针完整重合在一路的时辰有几回?都分辨是什么时光?你如何算出来的?
    12个球一个天平,现知道只有一个和其它的重量不同,问怎样称才能用三次就找到那个球。13个呢?(注意此题并未说明那个球的重量是轻是重,所以需要仔细考虑)
    Bootstrap4默认样式不对胃口?教你使用NPM+Webpack+SASS来定制
    Java 内存模型和 JVM 内存结构真不是一回事
    还在为垂直居中苦恼?CSS 布局利器 flexbox 轻轻松松帮你搞定
    这 3 个 Set 集合的实现有点简单,那来做个总结吧
    TreeMap 还能排序?分析下源码就明白了
    红黑树这个数据结构,让你又爱又恨?看了这篇,妥妥的征服它
    模拟实现 Tomcat 的核心模块:NIO,HTTP,容器和集群
    「福利」Java Swing 编写的可视化算法工程,包含树、图和排序
  • 原文地址:https://www.cnblogs.com/yuen/p/5033887.html
Copyright © 2020-2023  润新知