UISearchController - 搜索控制器
来源:互联网 发布:手机淘宝怎么售后 编辑:程序博客网 时间:2024/04/29 22:48
搜索控制器(简称UISC)
1、概述
一个UISC对象管理基于searchBar的搜索结果的展示。UISC对象通常与两个自定义的 view controller一起工作,第一个显示可搜索的内容,第二个显示搜索结果。第一个VC是主界面的一部分,第二个是你通过initWithSearchResultsController:初始化的VC。
UISC对象包含一个UISearchBar对象(searchBar),你必须把他分配给第一个view controller用来展示,UIC通常和 UITableView 一起来实现搜索功能,可以把UITableView的属性tableHeadView 设置为searchBar。
当你和search bar 交互的时候,UISC会通知它的属性searchResultsUpdater其遵循UISearchResultsUpdating 协议,你可以用该协议提供的方法搜索内容,并把搜索结果发送给results view controller。
2、属性 active
当用户点击搜索区域的时候,search controller 就会自动展示 search results controller( 搜索结果控制器),可以通过此值来判断搜索结果控制器是否已经展示出来。
if searchController.active { return searchList.count } return dataList.count}
3、属性 searchResultsController
当此值为nil的时候, search controller不会用单独的view controller 来展示搜索结果,这时候应该用包含搜索框和可搜索内容的view controller 来展示。
4、属性 searchBar:UISearchBar {get}
在展示搜索内容之前,应该把此 searchBar 显示在主控制器界面上。其为搜索内容的开始点。如果要自定义一个UISearchBar, 应该子类化UISearchController,进一步定制searchBar。
5、使用步骤
1)创建UISearchController实例(searchController)
//初始化searchController,如果searchResultsController设置为nil,那么将会用展示可搜索内容的vc来展示搜索结果 searchController = UISearchController(searchResultsController: nil)
2)设置searchController代理
//设置代理,其遵循UISearchControllerDelegate协议 searchController.delegate = self //设置搜索结果的更新者,其遵循UISearchResultsUpdating协议 searchController.searchResultsUpdater = self
3)设置searchController属性
//搜索时,背景模糊 searchController.obscuresBackgroundDuringPresentation = false //搜索时,背景变暗 searchController.dimsBackgroundDuringPresentation = false //搜索时,隐藏导航条 searchController.hidesNavigationBarDuringPresentation = false
4)实现代理方法
//实现UISearchResultsUpdating协议方法 updateSearchResultsForSearchController(searchController: UISearchController) {}
//根据需要实现UISearchControllerDelegate协议方法 willPresentSearchController(searchController: UISearchController) {} didPresentSearchController(searchController: UISearchController) {} willDismissSearchController(searchController: UISearchController) {} didDismissSearchController(searchController: UISearchController) {} presentSearchController(searchController: UISearchController) {}
UISearchControllerDelegate几个方法的执行顺序:
presentSearchController
willPresentSearchController
willDismissSearchController
didDismissSearchController
具体参考下图:
6、示例
例子采用UITableView + UISearchController 来实现一个简单的数字搜索功能。
完整代码如下:
//// ViewController.swift// UISearchControllerDemo//// Created by fengsh on 16/3/9.// Copyright © 2016年 fengsh. All rights reserved.//import UIKitclass ViewController: UIViewController{ // 表格 var tableView: UITableView! //搜索控制器 var searchController: UISearchController! //数据 var dataList: [String]! //搜索结果数据 var searchList: [String] = [] { //更新搜索结果数据的时候tableView重新加载 didSet { tableView.reloadData() } } let indentifier = "cell" override func viewDidLoad() { super.viewDidLoad() initData() let rect = CGRect(x: 0, y: 20, width: self.view.bounds.width, height: self.view.bounds.height) tableView = UITableView(frame: rect, style: UITableViewStyle.Plain) //注册复用的单元格 tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.indentifier) //设置tableView的代理,其遵循UITableViewDelegate协议 tableView.delegate = self //设置tableView的数据源,其遵循UITableViewDataSource协议 tableView.dataSource = self self.view.addSubview(tableView) //初始化searchController,如果searchResultsController设置为nil,那么将会用展示可搜索内容的vc来展示搜索结果 searchController = UISearchController(searchResultsController: nil) //设置代理,其遵循UISearchControllerDelegate协议 searchController.delegate = self //设置搜索结果的更新者,其遵循UISearchResultsUpdating协议 searchController.searchResultsUpdater = self //搜索时,背景模糊 searchController.obscuresBackgroundDuringPresentation = false //搜索时,背景变暗 searchController.dimsBackgroundDuringPresentation = false //搜索时,隐藏导航条 searchController.hidesNavigationBarDuringPresentation = false //搜索条显示在头部 tableView.tableHeaderView = searchController.searchBar }// 初始化数据 func initData() { dataList = [] for var i = 0; i < 20; i++ { //randomNumber是一个随机整数 let randomNumber = arc4random() let str = String(randomNumber) dataList.append(str) } }}//MARK:----实现UITableViewDataSource协议extension ViewController: UITableViewDataSource { //配置多少个分组 func numberOfSectionsInTableView(tableView: UITableView) -> Int { //如果用户点击了搜索区域,那么active = true if searchController.active { return searchList.count } return dataList.count } //配置每个分组有多少个单元格 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } //配置每个单元格 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(self.indentifier, forIndexPath: indexPath) if searchController.active { cell.textLabel?.text = searchList[indexPath.section] } else { cell.textLabel?.text = dataList[indexPath.section] } return cell }}//MARK:----实现UITableViewDelegate协议extension ViewController: UITableViewDelegate { func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { }}//MARK:----实现UISearchResultsUpdating协议extension ViewController: UISearchResultsUpdating { func updateSearchResultsForSearchController(searchController: UISearchController) { searchList = dataList.filter{ $0.containsString(searchController.searchBar.text!)} }}//MARK:----实现UISearchControllerDelegate协议extension ViewController: UISearchControllerDelegate { func willPresentSearchController(searchController: UISearchController) { print("willPresentSearchController") } func didPresentSearchController(searchController: UISearchController) { print("didPresentSearchController") } func willDismissSearchController(searchController: UISearchController) { print("willDismissSearchController") } func didDismissSearchController(searchController: UISearchController) { print("didDismissSearchController") } func presentSearchController(searchController: UISearchController) { print("presentSearchController") }}
- UISearchController - 搜索控制器
- iOS开发实战之搜索控制器UISearchController使用
- UISearchController搜索
- iOS开发 - 使用UISearchController跳转新控制器时搜索框依然出现
- OCUI界面设计:UISearchController 搜索
- iOS--搜索UISearchController&&UISearchDisplayController
- 搜索栏 UISearchController
- UISearchController实现搜索
- iOS: 搜索控件UISearchController
- 不用UIsearchController实现搜索功能
- IOS 用UISearchController搜索Tableview
- 搜索栏UISearchBar和UISearchController
- 搜索栏UISearchBar和UISearchController
- UISearchController进行网络实时搜索
- 搜索栏UISearchBar和UISearchController
- 70.UISearchController(搜索栏)
- 使用UISearchController 实现搜索框
- iOS开发-搜索栏 UISearchController
- 用Python实现KNN算法
- Message queue
- 2016年蓝桥杯:区间k大数查询
- Android和WCF通信 - 大数据压缩后传输
- 树莓派折腾录一. 必要的初始化(Raspberry Pi 2 model B + raspbian jessie)
- UISearchController - 搜索控制器
- MC9S12G128 内部flash与eeprom的读写配置
- casperjs在windows下抓取元素乱码
- unity 初次制作
- jenkins报错总结
- BZOJ_P3517 翻硬币(博弈+数学)
- Apache 虚拟主机 VirtualHost 配置
- 安卓的增量更新
- CSRF verification failed. Request aborted.