转在于 :http://www.cnblogs.com/lesliefang/p/3929677.html
UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类。
里面内置了一个 UITableView 用于显示搜索的结果。它可以和一个需要搜索功能的
controller 关联起来,其它的像原 TableView 和搜索结果 TableView 的切换, mask 的显示等等都
封装好了,使用起来非常非常的简单。特别是要实现全屏搜索时使用最多。
全屏搜索的意思是如果你用了 NavigationBar 当点击搜索框时 TableView 会自动弹上去覆盖
NavigationBar,达到一种全屏搜索的效果,这一切 UISearchDisplayController 都封装好了,如果自己
写就比较麻烦一些。
关键代码:
@interface MainViewController : UITableViewController{ NSArray *data; NSArray *filterData; UISearchDisplayController *searchDisplayController; }
- (void)viewDidLoad { [super viewDidLoad]; UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 44)]; searchBar.placeholder = @"搜索"; // 添加 searchbar 到 headerview self.tableView.tableHeaderView = searchBar; // 用 searchbar 初始化 SearchDisplayController // 并把 searchDisplayController 和当前 controller 关联起来 searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; // searchResultsDataSource 就是 UITableViewDataSource searchDisplayController.searchResultsDataSource = self; // searchResultsDelegate 就是 UITableViewDelegate searchDisplayController.searchResultsDelegate = self; }
/* * 如果原 TableView 和 SearchDisplayController 中的 TableView 的 delete 指向同一个对象 * 需要在回调中区分出当前是哪个 TableView */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.tableView) { return data.count; }else{ // 谓词搜索 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",searchDisplayController.searchBar.text]; filterData = [[NSArray alloc] initWithArray:[data filteredArrayUsingPredicate:predicate]]; return filterData.count; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId = @"mycell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } if (tableView == self.tableView) { cell.textLabel.text = data[indexPath.row]; }else{ cell.textLabel.text = filterData[indexPath.row]; } return cell; }
DEMO 下载:http://pan.baidu.com/s/1pJ8vvC3
新的搜索功能实现
iOS 8以后,苹果推荐使用UISearchController,如果在初始化的时候,没有设置searchResultsController,那么搜索结果就在代理对象控制器的表格中显示。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
接下来也是实现代理方法:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
因为搜索结果和正常情况使用的是同一个tableView,那么如何区分搜索时显示搜索的数据源呢?
通过UISearchController
的active属性即可判断,这是一个BOOL类型的属性。
示例代码:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
其实到这里,表格头部的搜索功能就完成了。
下面还有一些可能出现的Bug或场景解决方案。
1.如果我不需要实时显示搜索结果怎么处理?
显然,如果我们搜索功能的结果需要服务器返回,那么就不应该在用户输入每个字母的时候都去请求一次。应该是用户输入完关键字,点击搜索后再去搜索。
那么这时,就需要先实现UISearchBar 的UISearchBarDelegate
中的代理方法:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
然后,还必须实现的UISearchController
的UISearchResultsUpdating
中的代理方法:
- 1
- 2
- 3
- 4
- 5
可以看一下上面这个代理方法的注释:
- 1
- 2
所以需要在这里刷新一下tableView 来更新数据源,因为他们用的是同一个tableView展示数据。
怎么证明呢?
很简单,只需要在tableView 的数据源方法中打印一下tableView,看他们是否是同一个TableView对象即可。
- 1
- 2
- 3
- 4
- 5
- 6
2.设置搜索栏背景色
直接上代码了:
- 1
当然,在以前的实现方式里,也可以直接设置searchBar的barTintColor来修改搜索栏的背景色。(这个属性是iOS 7之后加的)
3.改变搜索栏 “取消”按钮和 光标的颜色
还是直接上代码:
- 1
4.使用UISearchController如何在搜索时将搜索栏移动到导航栏上
这个问题的解决方案需要设置两个地方:
第一,确保UISearchController 的hidesNavigationBarDuringPresentation
为YES(这个属性默认就是YES,你要确定你没修改过这个属性)。
第二,将当前控制器的definesPresentationContext
设置为YES。即: