UISearchController的使用
- (void)addSearchController
{
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.delegate = self;
_searchController.searchResultsUpdater = self;
// 必须要让searchBar自适应才会显示
[_searchController.searchBar sizeToFit];
_searchController.searchBar.delegate = self;
[_searchController.searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
_searchController.searchBar.backgroundImage = [UIImage imageWithColor:kAppBakgroundColor];
_searchController.searchBar.placeholder = @"用户ID/昵称";
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = NO;
//把searchBar 作为 tableView的头视图
self.tableView.tableHeaderView = _searchController.searchBar;
}
//把searchBar 作为 tableView的头视图
self.tableView.tableHeaderView = _searchController.searchBar;
#pragma mark - UISearchController的代理
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
if (self.searchResults.count) {
[self.searchResults removeAllObjects];
}
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"userId CONTAINS[c] %@",searchController.searchBar.text];
NSArray *array = [(NSArray *)self.searchArray filteredArrayUsingPredicate:searchPredicate];
self.searchResults = [NSMutableArray arrayWithArray:array];
//刷新表格
[self.tableView reloadData];
}
在tableView中使用时,可以根据_searchController.active的属性来判断是否显示搜索到的数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (_searchController.active) {
return [self.searchResults count];
}else{
id<IMAContactDrawerShowAble> drawer = [_contact objectAtIndex:section];
if ([self isDrawerFolded:drawer])
{
return 0;
}
else
{
return [drawer items].count;
}
}
}
参考地址:http://www.mamicode.com/info-detail-1156195.html
http://www.2cto.com/kf/201510/447287.html
UISearchController的使用的更多相关文章
- UISearchController 的用法[点击搜索框,自动到顶部]
//在ViewDidLoad里面如下代码 self.searchViewController = [[UISearchController alloc]initWithSearchResultsCon ...
- iOS UISearchController的使用
在iOS9中,UISearchDisplayController 已经被UISearchController替代.搜索框是一种常用的控件. 假设我们要满足下图的需求,产生100个“数字+三个随机字母” ...
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- UISearchController使用
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索 iOS8提供实现搜索功能的SDK:UISearchController(iOS8.0之后).UIS ...
- UItableview 添加 uisearchController
@property (nonatomic, strong) UISearchController* searchController; self.searchController = [[UISear ...
- UISearchController
搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UIS) 1.在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便 ...
- UITableView与UISearchController搜索及上拉加载,下拉刷新
#import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableVi ...
- iOS原生的搜索:UISearchController
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...
- UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...
- iOS8以后 UISearchController的用法
查了不少资料,都不太全,自己查看了apple文档,写了一份代码: 如下(只是界面): 1. 声明属性 @property (nonatomic, strong) UISearchController ...
随机推荐
- 【转】C# 解析JSON方法总结
http://blog.csdn.net/jjhua/article/details/51438317 主要参考http://blog.csdn.NET/joyhen/article/details/ ...
- 【转】Laravel+Angularjs+D3打造可视化数据,RESTful+Ajax
http://897371388.iteye.com/blog/1975351 大致思路也就是下面,由于最近在学Laravel也在学Angularjs,加上之前做的项目用到了d3. 原来的方案如下: ...
- PTA实验第一次作业
- linux 冒号的用途
用途说明 我们知道,在Linux系统中,冒号(:)常用来做路径的分隔符(PATH),数据字段的分隔符(/etc/passwd)等.其实,冒号(:)在Bash中也是一个内建命令,它啥也不做,是个空命令. ...
- nodejs net模块实现socket
var net = require('net'); var client = net.connect({port: 8080}, function() { console.log('连接到服务器!') ...
- 树莓派如何便捷的使用pi4j
问题的由来 pi4j用起来很方便,但是感觉pi4j库的命名太杂乱,啰嗦了,很容易弄混,而且好像没听说官方有自己的编译器.如果没有智能点的编辑器的话,写起来真要命,但是树莓派运行Eclipse不太现实, ...
- 日志案例分析(PV,UV),以及动态分区
1.实现的流程 需求分析 时间:日,时段 分区表:两级 PV UV 数据清洗 2015-08-28 18:19:10 字段:id,url,guid,tracktime 数据分析 导出 2.新建源数据库 ...
- VS2013编译Qt5.6.0静态库(乌合之众)
获取qt5.6.0源码包 直接去www.qt.io下载就好了,这里就不详细说了. 这里是我已经编译好的** 链接:http://pan.baidu.com/s/1pLb6wVT 密码: ak7y ** ...
- Github 与Git pages
基础git命令 设置username,email $ git config --global user.name "your name" $ git config --global ...
- Linux-modules software
简介 这里指的modules不是linux内核相关的module,只是用于软件多版本控制的一个开源软件包,比如说系统同时有neo4j的不同版本,使用modules软件就可以使得在需要的时候选择相应的软 ...