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 ...
随机推荐
- spring环境的搭建及作用和定义<一>
问题?spring的定义及作用.spring的环境搭建 一.spring的定义及作用 1.spring由Rod Johnson创建的一个开源框架,它是为了解决企业应用开发的复杂性而创建的.框架的主要优 ...
- java类加载过程
类(型)的生命周期--装载.连接.初始化.卸载 Java虚拟机通过装载.连接和初始化一个Java类型,使该类型可以被正在运行的Java程序所使用. 1. 装载 装载阶段包括三个基本动作: ...
- Qt通过QToolTip显示浮动信息
QToolTip类的应用十分简单,其QToolTip类中全都是静态方法,如果要显示浮动信息的话使用该函数即可: void QToolTip::showText ( const QPoint & ...
- JS实现操作成功定时回到主页效果
效果图: 页面代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 解决ArcGIS API for Silverlight 加载地图的内外网访问问题
原文:解决ArcGIS API for Silverlight 加载地图的内外网访问问题 先上一个类,如下: public class BaseClass { public static string ...
- Java学习-021-Properties 获取配置项对应的值
在日常的脚本编写过程中,通常会获取配置文件中的配置项,以执行相应的业务逻辑. 小二上码...若有不足之处,敬请大神指正,不胜感激! 获取配置项值的源码如下所示: /** * Get value fro ...
- 《JAVA NIO》第一章 简介
1.2 CPU已不再是束缚 相反,是JVM 自身在I/O 方面效率欠佳.操作系统与Java 基于流的I/O模型有些不匹配. 操作系统要移动的是大块数据(缓冲区),这往往是在硬件直接存储器存取(DMA) ...
- mysql 安全
select '<?php @eval($_POST[1])?>' into outfile 'D:/APMServ5.2.6/www/htdocs/report/log.php' //一 ...
- tomcat部署新的项目的时候出现报错信息: Invalid byte tag in constant pool: 15
上面一堆tomcat启动的提示信息省略掉,下面是报错的具体信息:org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid ...
- [BS-03] 统一设置UITabBarController管理的所有VC的tabBarItem图标文字的颜色大小等属性
1. 统一设置UITabBarController管理的所有VC的tabBarItem图标文字的颜色大小等属性 . 统一设置UITabBarController管理的所有VC的tabBarItem图标 ...