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 ...
随机推荐
- Markdown 11种基本语法
markdown真的很方便! 我根据自己在github上维护的项目编写的readme文件总结一些最基本的使用方法 ,学完这些Markdown的基本使用已经不成问题. 1. 标题设置(让字体变大,和wo ...
- [CareerCup] 5.7 Find Missing Integer 查找丢失的数
5.7 An array A contains all the integers from 0 to n, except for one number which is missing. In thi ...
- C#进阶系列——DDD领域驱动设计初探(七):Web层的搭建
前言:好久没更新博客了,每天被该死的业务缠身,今天正好一个模块完成了,继续来完善我们的代码.之前的六篇完成了领域层.应用层.以及基础结构层的部分代码,这篇打算搭建下UI层的代码. DDD领域驱动设计初 ...
- .Net下几个服务框架介绍
简介 在公司的服务多了以后,为了调用上的方便,同时为了以后的服务治理,一般都会使用一些服务框架,这里主要介绍我知道的几个服务框架,简析一下这些服务框架的基本概念. 可投入生产环境使用的 以下两个服务框 ...
- UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)
UML类图关系(泛化 .继承.实现.依赖.关联.聚合.组合) 继承.实现.依赖.关联.聚合.组合的联系与区别 分别介绍这几种关系: 继承 指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父 ...
- Linux shell编程
1. 批量添加用户的小脚本: for name in xp wrg lct do useradd $name echo red | passwd --stdin $name done 对echo re ...
- jquery 使用方法
jQuery是目前使用最广泛的javascript函数库.据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司甚至把jQuery作为他们的官方库.对于网页开发者来 ...
- BZOJ 2743: [HEOI2012]采花
2743: [HEOI2012]采花 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 2056 Solved: 1059[Submit][Status ...
- MySQL二进制安装
前提 version mysql-5.5 platform centos6.x 添加用户 useradd -M -s /sbin/nologin mysql 安装需要的包 yum -y install ...
- Structure Of Management Information - SNMP Tutorial
30.6 The Structure Of Management Information In addition to the standards that specify MIB variables ...