iOS--搜索UISearchController&&UISearchDisplayController
2016-01-12 11:11
543 查看
12rd,Jan,2016
打算实现一个带搜索的tableView,使用UISearchDisplayController时提示'UISearchDisplayerController' is deprecated. UISearchDisplayContrller has been replaced with UISearchController.所以本文将会通过两种方法实现改功能:
1. UISearchDisplayerController :实现iOS8以前的版本
2. UISearchController: 实现iOS8以后的版本
![](https://app.yinxiang.com/shard/s41/res/189fc348-69fc-4844-a10e-5bfe2e60abd1/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8A%E5%8D%8810.47.12.png)
1.UISearchDisplayController
1)首先拖一个Search Bar and Search Display Controller到tableView上面,拖进去之后点击查看Search Display Controller观察到该对象的绑定情况。
![](https://app.yinxiang.com/shard/s41/res/bddf29a5-54a8-42cb-9d13-905c86d4d807/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8A%E5%8D%8811.19.43.png)
2) 实现代理:
UISearchDisplayDelegate,UISearchBarDelegate
3)声明数据存储变量&&初始化数据:
NSArray *chatList; NSArray *resultList;
4)然后实现tableView的代理方法:numberOfScetionsInTableView, numberOfRowsInSection,cellForRowAtIndexPath 。分为两种情况,一种情况是默认显示的数据,一种是搜索结果要显示的数据(tableView == self.searchDisplayController.searchResultsTableView)
5)实现UISearchDisplayDelegate中的委托方法: 通过谓词方法过滤数据()
![](https://app.yinxiang.com/shard/s41/res/1dd9c877-c38e-44ba-92bd-7f048f40e16f/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8B%E5%8D%885.00.33.png)
2. UISearchController
1) 实现代理:
UISearchResultsUpdating
2) 声明数据存储变量&&初始化数据:
NSMutableArray *chatList; NSMutableArray *resultList;
@property (nonatomic,
strong) UISearchController *searchController;
3) 初始化searchController:
![](https://app.yinxiang.com/shard/s41/res/8a771bd1-1165-42ad-b748-fd63e2733021/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8B%E5%8D%886.28.38.png)
4) 实现tableView的代理方法:numberOfScetionsInTableView, numberOfRowsInSection,cellForRowAtIndexPath 。分为两种情况,一种情况是默认显示的数据,一种是搜索结果要显示的数据。判断方法self.searchController.active 为真则是搜索结果的情况。
5)实现UISearchResultsUpdating的代理方法:
updateSearchResultsForSearchController即可
![](https://app.yinxiang.com/shard/s41/res/b85243b4-718e-4baf-acfb-153ff8925cf5/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8B%E5%8D%886.24.07.png)
参考文献:
打算实现一个带搜索的tableView,使用UISearchDisplayController时提示'UISearchDisplayerController' is deprecated. UISearchDisplayContrller has been replaced with UISearchController.所以本文将会通过两种方法实现改功能:
1. UISearchDisplayerController :实现iOS8以前的版本
2. UISearchController: 实现iOS8以后的版本
![](https://app.yinxiang.com/shard/s41/res/189fc348-69fc-4844-a10e-5bfe2e60abd1/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8A%E5%8D%8810.47.12.png)
1.UISearchDisplayController
1)首先拖一个Search Bar and Search Display Controller到tableView上面,拖进去之后点击查看Search Display Controller观察到该对象的绑定情况。
![](https://app.yinxiang.com/shard/s41/res/bddf29a5-54a8-42cb-9d13-905c86d4d807/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8A%E5%8D%8811.19.43.png)
2) 实现代理:
UISearchDisplayDelegate,UISearchBarDelegate
3)声明数据存储变量&&初始化数据:
NSArray *chatList; NSArray *resultList;
4)然后实现tableView的代理方法:numberOfScetionsInTableView, numberOfRowsInSection,cellForRowAtIndexPath 。分为两种情况,一种情况是默认显示的数据,一种是搜索结果要显示的数据(tableView == self.searchDisplayController.searchResultsTableView)
5)实现UISearchDisplayDelegate中的委托方法: 通过谓词方法过滤数据()
![](https://app.yinxiang.com/shard/s41/res/1dd9c877-c38e-44ba-92bd-7f048f40e16f/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8B%E5%8D%885.00.33.png)
2. UISearchController
1) 实现代理:
UISearchResultsUpdating
2) 声明数据存储变量&&初始化数据:
NSMutableArray *chatList; NSMutableArray *resultList;
@property (nonatomic,
strong) UISearchController *searchController;
3) 初始化searchController:
![](https://app.yinxiang.com/shard/s41/res/8a771bd1-1165-42ad-b748-fd63e2733021/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8B%E5%8D%886.28.38.png)
4) 实现tableView的代理方法:numberOfScetionsInTableView, numberOfRowsInSection,cellForRowAtIndexPath 。分为两种情况,一种情况是默认显示的数据,一种是搜索结果要显示的数据。判断方法self.searchController.active 为真则是搜索结果的情况。
5)实现UISearchResultsUpdating的代理方法:
updateSearchResultsForSearchController即可
![](https://app.yinxiang.com/shard/s41/res/b85243b4-718e-4baf-acfb-153ff8925cf5/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-01-12%20%E4%B8%8B%E5%8D%886.24.07.png)
参考文献:
ios UISearchController
iOS开发-搜索栏UISearchBar和UISearchController
相关文章推荐
- [Google] URL query with wild card
- [LeetCode] Longest Consecutive Sequence, Solution
- 使用UIImageView的画线方法
- [LeetCode] Unique Paths 解题报告
- [LeetCode] Unique Paths II 解题报告
- UINavigationController 与 UITabBarController
- [Leetcode] Distinct Subsequences 解题报告
- How to check in Windows if you are using UEFI
- Request processing failed; nested exception is java.lang.NullPointerException] with root cause
- Burp Suite使用介绍——Proxy功能(七)
- Burp Suite使用介绍——Proxy功能(六)
- 深入理解require与require_once与include以及include_once的区别
- 如何在UIViewController中 使用UIRefreshControl
- Burp Suite使用介绍——Proxy功能(五)
- jqGrid ---Grid can not be used in this ('quirks...
- iOS--- UITableView 底部添加按钮
- Android 讲解:基本UI 控件
- iOS 在UILabel显示不同的字体和颜色(ios6 and later)
- 本人对Material Requirements Planning模块的一些了解
- 页面对json返回自动result:<pre style="word-wrap: break-word; white-space: pre-wrap;">{"flag":"true"}</pre>问题