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使用的更多相关文章

  1. UISearchController 的用法[点击搜索框,自动到顶部]

    //在ViewDidLoad里面如下代码 self.searchViewController = [[UISearchController alloc]initWithSearchResultsCon ...

  2. iOS UISearchController的使用

    在iOS9中,UISearchDisplayController 已经被UISearchController替代.搜索框是一种常用的控件. 假设我们要满足下图的需求,产生100个“数字+三个随机字母” ...

  3. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  4. UItableview 添加 uisearchController

    @property (nonatomic, strong) UISearchController* searchController; self.searchController = [[UISear ...

  5. UISearchController

    搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UIS) 1.在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便 ...

  6. UITableView与UISearchController搜索及上拉加载,下拉刷新

    #import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableVi ...

  7. iOS原生的搜索:UISearchController

    iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...

  8. 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 ...

  9. iOS8以后 UISearchController的用法

    查了不少资料,都不太全,自己查看了apple文档,写了一份代码: 如下(只是界面): 1. 声明属性 @property (nonatomic, strong) UISearchController ...

随机推荐

  1. C#基础系列——反射笔记

    前言:使用反射也有几年了,但是一直觉得,反这个概念很抽象,今天有时间就来总结下这个知识点. 1.为什么需要反射: 最初使用反射的时候,作为小菜总是不理解,既然可以通过new 一个对象的方式得到对象,然 ...

  2. Xml,Json,Hessian,Protocol Buffers序列化对比

    简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...

  3. DbUtility v3

    DbUtility v3 历史 七年前,也就是2007年,我在博客园写了一篇博文,开源并发布了恐怕是我第一个开源项目,DbUtility.其设计的初衷就是为了简化ADO.NET繁琐的数据库访问过程,提 ...

  4. JavaScript语法规范

    推荐的JavaScript编码规范 阅读 247 评论 0 喜欢 0 作为前端开发人员,我相信每一个人都或多或少的用到原生的JavaScript,也正是因为用的人多,导致编码风格也是多种多样的,而不规 ...

  5. 在哪可以Sigmaplot 13下载免费版的

    SigmaPlot是一款科学的数据分析和绘图软件,可以进行精密绘图.数据分析.自动化管理数据和创建网络图表,具备制作和发行品质图表的水平,该软件被广泛地运用在多个领域.现今SigmaPlot软件日益受 ...

  6. MacTalk阅读有感

    MacTalk by 池建强 高手的思维境界 -贴地气 -有思想 技术普及 文字是表达人思想的载体,池老师将自己的经历,经验分享给大家,很值得大家学习,虽然我只是个初出茅庐的小菜,现在有指路明灯一样. ...

  7. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  8. .NET DateTime 显示格式

    备注     format 参数应包含单个格式说明符 (请参阅 标准日期和时间格式字符串) 或自定义格式模式 (请参阅 Cadenas con formato de fecha y hora pers ...

  9. [UOJ30/Codeforces Round #278 E]Tourists

    传送门 好毒瘤的一道题QAQ,搞了好几好几天. UOJ上卡在了53个点,CF上过了,懒得优化常数了 刚看时一眼Tarjan搞个强连通分量然后缩点树链剖分xjb搞搞就行了,然后写完了,然后WA了QAQ. ...

  10. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...