前言

	NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearchController")
@interface UISearchDisplayController : NSObject @available(iOS, introduced=3.0, deprecated=8.0, message="UISearchDisplayController has been replaced with
UISearchController")
public class UISearchDisplayController : NSObject NS_CLASS_AVAILABLE_IOS(8_0) @interface UISearchController : UIViewController
<UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>
@available(iOS 8.0, *) public class UISearchController : UIViewController,
UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning
  • 在 iOS 8.0 以上版本中, 我们可以使用 UISearchController 来非常方便地在 UITableView 中添加搜索框. 而在之前版本中, 我们还是必须使用 UISearchDisplayController + UISearchBar 的组合方式.

  • 我们创建的 tableView 和搜索控制器创建的 tableView 都会走代理方法,需要在代理方法中判断响应代理方法的 tableView 是哪一个,如果响应代理方法的 tableView 不是我创建的,说明一定是搜索控制器创建的。在 iOS 8.0 以下版本中需使用 tableView == myTableView 判断,在 iOS 8.0 以上版本中需使用 mySearchController.active 判断。

1、搜索框的创建

1.1 在 iOS 8.0 以下版本中

  • Objective-C

    • 遵守协议 UISearchDisplayDelegate

    • 搜索结果数组初始化

      	// 声明搜索结果存放数组
      @property(nonatomic, retain)NSMutableArray *mySearchResultArray; // 初始化搜索结果存放数组
      mySearchResultArray = [[NSMutableArray alloc] init];
    • searchDisplayController 初始化

      	// 声明搜索控制器,自带一个表格视图,用来展示搜索结果,必须设置为全局变量
      @property(nonatomic, retain)UISearchDisplayController *mySearchDisplayController; // 实例化搜索条
      UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; // 实例化搜索控制器对象
      mySearchDisplayController = [[UISearchDisplayController alloc]
      initWithSearchBar:searchBar
      contentsController:self];
      // 设置搜索控制器的代理
      mySearchDisplayController.delegate = self; // 为搜索控制器自带 tableView 指定代理
      mySearchDisplayController.searchResultsDelegate = self;
      mySearchDisplayController.searchResultsDataSource = self; // 将搜索条设置为 tableView 的表头
      myTableView.tableHeaderView = searchBar;
    • UISearchDisplayDelegate 协议方法

      	// 更新搜索结果
      /*
      只要搜索框的文字发生了改变,这个方法就会触发。searchString 为搜索框内输入的内容。
      */
      - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { // 清空上一次搜索的内容
      [mySearchResultArray removeAllObjects]; // 将搜索的结果存放到数组中
      for (NSArray *subArray in myDataArray) {
      for (NSString *str in subArray) { NSRange range = [str rangeOfString:searchString]; if (range.length) {
      [mySearchResultArray addObject:str];
      }
      }
      }
      return YES;
      }
    • UITableView 协议方法

      	// 设置分段头标题
      - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (tableView == myTableView) {
      return [NSString stringWithFormat:@"%c", (char)('A' + section)];
      }
      return @"搜索结果";
      } // 设置分段数
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (tableView == myTableView) {
      return myDataArray.count;
      }
      return 1;
      } // 设置行数
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == myTableView) {
      return [[myDataArray objectAtIndex:section] count];
      }
      return mySearchResultArray.count;
      } // 设置每段显示的内容
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testIdentifier"]; if (!cell) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testIdentifier"];
      } if (tableView == myTableView) {
      cell.textLabel.text = [[myDataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
      }
      else {
      cell.textLabel.text = [mySearchResultArray objectAtIndex:indexPath.row];
      } return cell;
      }
  • Swift

    • 遵守协议 UISearchDisplayDelegate

    • 搜索结果数组初始化

      	// 初始化搜索结果存放数组
      var mySearchResultArray:[String] = Array()
    • searchDisplayController 初始化

      	// 声明搜索控制器,自带一个表格视图,用来展示搜索结果,必须设置为全局变量
      var mySearchDisplayController:UISearchDisplayController! // 实例化搜索条
      let searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44)) // 实例化搜索控制器对象
      mySearchDisplayController = UISearchDisplayController(searchBar: searchBar,
      contentsController: self) // 设置搜索控制器的代理
      mySearchDisplayController.delegate = self // 为搜索控制器自带 tableView 指定代理
      mySearchDisplayController.searchResultsDelegate = self
      mySearchDisplayController.searchResultsDataSource = self // 将搜索条设置为 tableView 的表头
      myTableView.tableHeaderView = searchBar
    • UISearchDisplayDelegate 协议方法

      	// 更新搜索结果
      /*
      只要搜索框的文字发生了改变,这个方法就会触发。searchString 为搜索框内输入的内容
      */
      func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool { // 清空上一次搜索的内容
      mySearchResultArray.removeAll() // 将搜索的结果存放到数组中
      for subArray in myDataArray {
      for str in subArray { let range:NSRange = (str as NSString).rangeOfString(searchString!) if range.length != 0 {
      mySearchResultArray.append(str)
      }
      }
      } return true
      }
    • UITableView 协议方法

      	// 设置分段头标题
      func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { if tableView == myTableView {
      return "\(Character(UnicodeScalar(65 + section)))"
      }
      return "搜索结果"
      } // 设置分段数
      func numberOfSectionsInTableView(tableView: UITableView) -> Int { if tableView == myTableView {
      return myDataArray.count
      }
      return 1
      } // 设置行数
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView == myTableView { return myDataArray[section].count
      }
      return mySearchResultArray.count
      } // 设置每段显示的内容
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("testIdentifier") if cell == nil {
      cell = UITableViewCell(style: .Default, reuseIdentifier: "testIdentifier")
      } if tableView == myTableView {
      cell!.textLabel?.text = myDataArray[indexPath.section][indexPath.row]
      }
      else {
      cell!.textLabel?.text = mySearchResultArray[indexPath.row]
      } return cell!
      }

1.2 在 iOS 8.0 及以上版本中

  • Objective-C

    • 遵守协议 UISearchResultsUpdating

    • 搜索结果数组初始化

      	// 声明搜索结果存放数组
      @property(nonatomic, retain)NSMutableArray *mySearchResultArray; // 初始化搜索结果存放数组
      mySearchResultArray = [[NSMutableArray alloc] init];
    • searchController 初始化

      	// 声明搜索控制器,自带一个表格视图控制器,用来展示搜索结果,必须设置为全局变量
      @property(nonatomic, retain)UISearchController *mySearchController; // 实例化搜索控制器
      mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil]; // 设置搜索代理
      mySearchController.searchResultsUpdater = self; // 设置搜索条大小
      [mySearchController.searchBar sizeToFit]; // 设置搜索期间背景视图是否取消操作,default is YES
      mySearchController.dimsBackgroundDuringPresentation = NO; // 设置搜索期间是否隐藏导航条,default is YES
      mySearchController.hidesNavigationBarDuringPresentation = NO; // 将 searchBar 添加到表格的开头
      myTableView.tableHeaderView = mySearchController.searchBar;
    • UISearchResultsUpdating 协议方法

      	// 更新搜索结果
      /*
      只要搜索框的文字发生了改变,这个方法就会触发。searchController.searchBar.text 为搜索框内输入的内容
      */
      - (void)updateSearchResultsForSearchController:(UISearchController *)searchController { // 清除上一次的搜索结果
      [mySearchResultArray removeAllObjects]; // 将搜索的结果存放到数组中
      for (NSArray *subArray in myDataArray) {
      for (NSString *str in subArray) { NSRange range = [str rangeOfString:searchController.searchBar.text]; if (range.length) {
      [mySearchResultArray addObject:str];
      }
      }
      } // 重新加载表格视图,不加载的话将不会显示搜索结果
      [myTableView reloadData];
      }
    • UITableView 协议方法

      	// 设置分段头标题
      - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (mySearchController.active) { return @"搜索结果";
      }
      return [NSString stringWithFormat:@"%c", (char)('A' + section)];
      } // 设置分段数
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (mySearchController.active) { return 1;
      }
      return myDataArray.count;
      } // 设置行数
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (mySearchController.active) { return mySearchResultArray.count;
      }
      return [[myDataArray objectAtIndex:section] count];
      } // 设置每段显示的内容
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testIdentifier"]; if (!cell) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testIdentifier"];
      } if (mySearchController.active) {
      cell.textLabel.text = [mySearchResultArray objectAtIndex:indexPath.row];
      }
      else {
      cell.textLabel.text = [[myDataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
      } return cell;
      }
  • Swift

    • 遵守协议 UISearchResultsUpdating

    • 搜索结果数组初始化

      	// 初始化搜索结果存放数组
      var searchResultArray:[String] = Array()
    • searchController 初始化

      	// 声明搜索控制器,自带一个表格视图控制器,用来展示搜索结果,必须设置为全局变量
      var mySearchController:UISearchController! // 实例化搜索控制器
      mySearchController = UISearchController(searchResultsController: nil) // 设置搜索代理
      mySearchController.searchResultsUpdater = self // 设置搜索条大小
      mySearchController.searchBar.sizeToFit() // 设置搜索期间背景视图是否取消操作,default is YES
      mySearchController.dimsBackgroundDuringPresentation = false // 设置搜索期间是否隐藏导航条,default is YES
      mySearchController.hidesNavigationBarDuringPresentation = false // 将 searchBar 添加到表格的开头
      myTableView.tableHeaderView = mySearchController.searchBar
    • UISearchResultsUpdating 协议方法

      	// 更新搜索结果
      /*
      只要搜索框的文字发生了改变,这个方法就会触发。searchController.searchBar.text 为搜索框内输入的内容
      */
      func updateSearchResultsForSearchController(searchController: UISearchController) { // 清除上一次的搜索结果
      searchResultArray.removeAll() // 将搜索的结果存放到数组中
      for subArray in myDataArray {
      for str in subArray { let range:NSRange = (str as NSString).rangeOfString(searchController.searchBar.text!) if range.length != 0 {
      searchResultArray.append(str)
      }
      }
      } // 重新加载表格视图,不加载的话将不会显示搜索结果
      myTableView.reloadData()
      }
    • UITableView 协议方法

      	// 设置分段头标题
      func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { if mySearchController.active {
      return "搜索结果"
      }
      return "\(Character(UnicodeScalar(65 + section)))"
      } // 设置分段数
      func numberOfSectionsInTableView(tableView: UITableView) -> Int { if mySearchController.active { return 1
      }
      return myDataArray.count
      } // 设置行数
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if mySearchController.active { return searchResultArray.count
      }
      return myDataArray[section].count
      } // 设置每段显示的内容
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("testIdentifier") if cell == nil {
      cell = UITableViewCell(style: .Default, reuseIdentifier: "testIdentifier")
      } if mySearchController.active {
      cell!.textLabel?.text = searchResultArray[indexPath.row]
      }
      else {
      cell!.textLabel?.text = myDataArray[indexPath.section][indexPath.row]
      } return cell!
      }

iOS - UISearchController的更多相关文章

  1. iOS UISearchController 的使用方法

    iOS UISearchController 的使用方法 UISearchController 让用户在 UISearchBar 上输入搜索关键词,展示搜索结果或者进行其他操作.UISearchCon ...

  2. iOS UISearchController的使用

    在iOS9中,UISearchDisplayController 已经被UISearchController替代.搜索框是一种常用的控件. 假设我们要满足下图的需求,产生100个“数字+三个随机字母” ...

  3. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  4. iOS UISearchBar UISearchController

    搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是 ...

  5. iOS中的两种搜索方式UISearchDisplayController和UISearchController

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...

  6. iOS中 UISearchController 搜索栏 UI技术分享

    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...

  7. iOS开发-搜索栏UISearchBar和UISearchController

    iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...

  8. [转] iOS开发-搜索栏UISearchBar和UISearchController

    原文网址: http://www.cnblogs.com/xiaofeixiang/p/4273620.html?utm_source=tuicool iOS中UISearchDisplayContr ...

  9. iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

随机推荐

  1. Hibernate,JPA注解@EmbeddedId

    定义组合主键的几种语法: 将组件类注解为@Embeddable,并将组件的属性注解为@Id 将组件的属性注解为@EmbeddedId 将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为 ...

  2. JavaEE基础(二十七)/反射、JDK新特性

    1.反射(类的加载概述和加载时机) A:类的加载概述 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载  就是指将class文件读入 ...

  3. samba服务器源码安装(非rpm)

    首先我们创建一个文档,边安装配置samba,边写教程. 从www.samba.org下载samba最新源码包,我下载的是samba-3.0.7.tar.gz,把它放在我的目录的中/root/lova/ ...

  4. tomcat 启动时参数设置说明

    使用Intellij idea 其发动tomcat时会配置启动vm options :-Xms128m -Xmx768m -XX:PermSize=64M -XX:MaxPermSize=512m. ...

  5. ajax 无刷新分页

    //ajax 无刷新分页1.前台要做的 滑动时 当前page+1,通过page ajax请求后台接口获取数据将数据进行拼装;2.后台要做的 做分页接口返回json数据前台判断触发请求条件: var p ...

  6. Android 5.1 - 状态栏充电标志问题

    Android 5.1 Ubuntu14.04  SourceInsigh 电量已满,插着USB头,观察Settings - Battery,电量为100%,状态为full,但仍有充电图标rust 之 ...

  7. AndroidManifest.xml 详解

    第1部分 标签库+包路径+版本控制 <manifest xmlns:android="http://schemas.android.com/apk/res/android" ...

  8. TCP数据流稳定性--TCP分片,重组及乱序

    http://www.cnblogs.com/derekchen/archive/2009/07/15/1524415.html 1.IP分片的情况.IP软件包有一个[分片]和[重组]模块,一个IP数 ...

  9. python-学习笔记之-Day5 双层装饰器 字符串格式化 python模块 递归 生成器 迭代器 序列化

    1.双层装饰器 #!/usr/bin/env python # -*- coding: utf-8 -*- # author:zml LOGIN_INFO = False IS_ADMIN = Fal ...

  10. C#中通过三边长判断三角形类型(三角形测试用例)

    对于<编程之美>P292上关于三角形测试用例的问题,题目是这样的: 输入三角形的三条边长,判断是否能构成一个三角形(不考虑退化三角形,即面积为零的三角形),是什么样的三角形(直角.锐角.钝 ...