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 ...
随机推荐
- Linq to Xml读取复杂xml(带命名空间)
前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...
- 新的一年快开始了,学点新东西吧,从React开始(一)
ReactJS是Facebook出的前端View框架,好东西啊. 看看它的说明: 仅仅是UI 许多人使用React作为MVC架构的V层. 尽管React并没有假设过你的其余技术栈, 但它仍可以作为一个 ...
- mysql Workbench 执行删除命令
SET SQL_SAFE_UPDATES = 0;delete from table1; SET SQL_SAFE_UPDATES = 1;
- JavaScript的理解记录(6)
---接上篇: 四.CSS相关: 1.CSS不支持注释// 支持注释/* */ 2. 几种浏览器厂商前缀: Firefox : -moz-; Chrome:-webkit- ; IE: ...
- 【C#】C#容易忽视的错误
1.string 拼接站内存,前提是字符串比较多的时候string 字符串类型拼接占内存,解决方法就是用 StringBuilder和String.Format2.不知道内置的验证数据类型的方法. ; ...
- 即使用ADO.NET,也要轻量级动态生成更新SQL,比Ormlite性能更高
先上测试结果: //测试1000次针对同一个表同一个字段更新,比Ormlite平均快2.34倍 //生成SQL+ExecuteNonQuery Ormlite 倍数 //6513ms 15158ms ...
- 未封装的js放大镜特效
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>j ...
- .Net Core Linux centos7行—vscode开发,linux部署运行
前面搭建好啦linux运行环境,下面搭建windows下的开发环境.并完成调试 参考地址:https://www.microsoft.com/net/core#windows. 按照步骤来就好.安装. ...
- js获取域名
<script language="javascript">//获取域名host = window.location.host;host2=document.domai ...
- SHOI2016游记&滚粗记&酱油记
Day0 学校刚期中考完,全科血崩,感觉这次真要考不到一本线了tat 晚上写了个可持久化trie的题,也懒得敲板子(上个礼拜都敲过了),就碎叫了 Day1 上午起床吃饭水群看球,吃完中饭就去考场了. ...