UISearchController使用
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索
iOS8提供实现搜索功能的SDK:UISearchController(iOS8.0之后)、UISearchDisplayController(iOS8.0之前,iOS8.0之后不建议再使用)。
遵守UISearchResultsUpdating协议
@interface ZWRecommendationFocusViewController()<UISearchResultsUpdating>
@property (strong, nonatomic) NSArray *list;
@property (strong, nonatomic) NSMutableArray *searchList;
@property (strong, nonatomic)UISearchController *searchController;
@end
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return (!self.searchController.active)?:;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (!self.searchController.active)?self.list.count:self.searchList.count;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ZWFocusList *list=(!self.searchController.active)?self.list[indexPath.row]:self.searchList[indexPath.row];
static NSString *identifier = @"cell";
ZWFocusListTableViewCell *cell = [ZWFocusListTableViewCell cellWithTableView:tableView identifier:identifier];
cell.list = list;
return cell;
}
/**
* UISearchController作为UITableView的tableHeaderView
*/
-(void)addSearchBar
{
//创建UISearchController对象
UISearchController *searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
//是否添加半透明覆盖层。为YES,搜索时,背景会变暗(背景变模糊)。默认为YES
searchController.dimsBackgroundDuringPresentation = NO;
//是否隐藏导航栏。为YES,搜索时,UINavigationBar会隐藏。默认为YES
searchController.hidesNavigationBarDuringPresentation = YES;
//NO表示UISearchController在present时,可以覆盖当前controller。默认为NO
searchController.definesPresentationContext = NO;
//搜索栏表头视图
self.tableView.tableHeaderView = searchController.searchBar; [searchController.searchBar sizeToFit];
searchController.searchResultsUpdater = self; searchController.searchBar.placeholder=@"请输入名称或者数字";
searchController.searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
// searchController.searchBar.showsCancelButton=YES;//显示取消按钮
//自适应
[searchController.searchBar sizeToFit];
// searchController.searchBar.frame = CGRectMake(0, 0, 0, 44);
// searchController.searchBar.prompt=@"请输入关键字";
self.searchController = searchController;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchString = self.searchController.searchBar.text;
//NSPredicate 谓词
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"theme_name LIKE[c] %@", searchString];
if (self.searchList!= nil) {
[self.searchList removeAllObjects];
}
//过滤数据
self.searchList= [NSMutableArray arrayWithArray:[self.list filteredArrayUsingPredicate:preicate]];
//刷新表格
[self.tableView reloadData];
}
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 ...
- 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语言程序代码(zz)
先贴在这了,后面应该用得着 http://zhidao.baidu.com/link?url=iw-hcd_tLpRtf4r2Kh-NmDPaQ10UdlunBQUWaz14J-eNEq5fw-y83 ...
- AppBox升级进行时 - 如何向OrderBy传递字符串参数(Entity Framework)
AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. Entity Framework提供的排序功能 再来回顾一下上篇文章,加载用户 ...
- Css-深入学习之弧形切角矩形
本文是作者从别的网站和文章学习了解的知识,简单做了个笔记,想要学习更多的可以参考这里:[css进阶]伪元素的妙用--单标签之美,奇思妙想 (弧形切角矩形) 代码: width: 180px; heig ...
- Tp缓存
系统默认的缓存方式是采用File方式缓存,我们可以在项目配置文件里面定义其他的缓存方式,例如,修改默认的缓存方式为Xcache(当然,你的环境需要支持Xcache) 对于File方式缓存下的缓存目录下 ...
- 我的第一篇blog
加入博客园两年多了,学习.从事编程也两年多了,一直是在网上找资料,都没有认真写写博客. 博客园里面好多功能都还不会用,今天起我也要在博客园写自己的blog了.感觉很高大上的样纸!!
- 用类(function(){})()实现点击显示index索引值的详解
code: <script type="text/javascript"> ; i < ; i++){ var btn = document.createElem ...
- android开发之线程
线程(android) 在java中我们学习了线程,线程,是进程的一个单位,在程序要运行时,会开启线程,运行程序,我们要创建线程就需要我们去继承接口Thread或者实现Runabl ...
- c# 文本框只能输入数字
<input type="text" onKeyUp="value=value.replace(/\D/g,'')" /> onKeyUp: 当输 ...
- bzoj4726【POI2017】Sabota?
首先可以推出来如果i没有带头叛变,那么i的父亲也一定不会带头叛变,证明显然 所以最劣情况初始的叛徒肯定是叶子,并且带头叛变的人一定是从某个叶子往上走一条链 f[i]表示i不带头叛变的话最小的x 那么我 ...
- Python【第二章】:Python的数据类型
基本数据类型 一.整型 如: 18.73.84 二.长整型 如:2147483649.9223372036854775807 三.浮点型 如:3.14.2.88 四.字符串 如:'wupeiqi'.' ...