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 ...
随机推荐
- Python学习之day2
1.执行Python脚本时打印的字符有颜色 print "\033[32;1mhello\033[0m" #打印绿色 print "\033[31;1mhello\033 ...
- C# 在类中反射
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Wind ...
- HTML+CSS知识点总结
转自:http://blog.csdn.net/qiushi_1990/article/details/40260447?utm_source=tuicool&utm_medium=refer ...
- DB&SQL备忘
DB2最佳分页语句 SELECT * FROM ( SELECT inner2_.*, ROWNUMBER() OVER(ORDER BY ORDER OF inner2_) AS rownumber ...
- BZOJ4540 [Hnoi2016]序列
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- jmeter测试 常用数据库的性能
在线程组中设置线程属性,执行次数=线程数*循环次数 本次JOB共插入了5W条记录,从14:56:46开始到15:01:29结束共耗时343s,平均145.8条/s. 同理sql sever:从15:2 ...
- 介绍对称加密算法,最常用的莫过于DES数据加密算法
DES DES-Data Encryption Standard,即数据加密算法.是IBM公司于1975年研究成功并公开发表的.DES算法的入口参数有三个:Key.Data.Mode.其中Key为8个 ...
- python学习笔记-(十五)RabbitMQ队列
rabbitMQ是消息队列:想想之前的我们学过队列queue:threading queue(线程queue,多个线程之间进行数据交互).进程queue(父进程与子进程进行交互或者同属于同一父进程下的 ...
- C#中的多态性
1.重载(overload) public void Sleep() { Console.WriteLine("Animal睡觉"); } public int Sleep(int ...
- asp rs开启关闭问题
使用rs.close关闭后,可以直接用rs.open来打开数据表:如果用了set rs = nothing 从内存中清除rs对象,再次加载rs对象就需要使用set rs=server.createob ...