搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
1.searchResultsUpdater:设置显示搜索结果的控制器
|
1
|
_mySearchController.searchResultsUpdater = self; |
2.dimsBackgroundDuringPresentation:设置开始搜索时背景显示与否
|
1
|
_mySearchController.dimsBackgroundDuringPresentation = NO; |
3.[searchBar sizeToFit]:设置searchBar位置自适应
|
1
|
[_mySearchController.searchBar sizeToFit]; |
4.设置searchBar为UITableView的头部视图
|
1
|
self.myTableView.tableHeaderView = self.mySearchController.searchBar; |
5.UISearchResultsUpdating:代理方法
#import "SearchViewController.h"
@interface ShareViewController ()<UISearchResultsUpdating,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *visableArray;//可见的
@property (nonatomic, strong) NSMutableArray *filterArray;//滤波器
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) UISearchController *mySearchController;
@end
@implementation SearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initial];
}
- (void)initial{
self.dataSourceArray = [NSMutableArray array];
self.filterArray = [NSMutableArray array];
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 4; j++) {
NSString *str = [NSString stringWithFormat:@"%c%d", 'A'+i, j];
[self.dataSourceArray addObject:str];
}
}
self.visableArray = self.dataSourceArray;
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource = self;
[self.view addSubview:_myTableView];
self.mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_mySearchController.searchResultsUpdater = self;
_mySearchController.dimsBackgroundDuringPresentation = NO;
[_mySearchController.searchBar sizeToFit];
self.myTableView.tableHeaderView = self.mySearchController.searchBar;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (!_visableArray || _visableArray.count == 0) {
_visableArray = _dataSourceArray;
}
return _visableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
}
cell.textLabel.text = [_visableArray objectAtIndex:indexPath.row];
return cell;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *filterString = searchController.searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [c] %@", filterString];
self.visableArray = [NSMutableArray arrayWithArray:[self.dataSourceArray filteredArrayUsingPredicate:predicate]];
[self.myTableView reloadData];
}
搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)的更多相关文章
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- iOS 搜索框控件 最简单的dome
刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...
- iOS8之后搜索框的常规实例
1.在一个二级导航控制器中添加一个UITableviewController作为子控制器 2.UITableviewController.tableView 作为展示结果 3.利用iOS之后的UISe ...
- iOS 新浪微博-2.0 搜索框/标题带箭头/下拉菜单
不管是搜索框还是下拉菜单,我们都需要对背景的图片进行拉伸.定义一个Category分类对图片进行操作. UIImage+Effect.h #import <UIKit/UIKit.h> @ ...
- 使用 Vue.js 2.0+ Vue-resource 模仿百度搜索框
使用 Vue.js 2.0 模仿百度搜索框 <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...
- 搜索框(Thinkphp5.0)
1.普通关键词搜索框 模板部分代码: <form name='searchform' action='/index.php/module/controller/search' method='g ...
- iOS搜索框
在iOS8以前搜索框是作为一个控件添加到TableViewController中, 有系统自带的搜索变量self.searchDisplayController 遵守一个搜索显示的协议<UISe ...
- iOS学习之NSPredictae及搜索框的实现
NSPredicate Predicate 即谓词逻辑, Cocoa框架中的NSPredicate用于查询,作用是从数据堆中根据条件进行筛选.计算谓词之后返回的结果永远为BOOL类型的值,当程序使用谓 ...
随机推荐
- BZOJ3881 : [Coci2015]Divljak
对Alice的所有串构造AC自动机,并建出Fail树 每当Bob添加一个串时,在AC自动机上走,每走到一个点,就把它到根路径上所有点的答案+1 需要注意的是每次操作,相同的点只能被加一次 所以在需要操 ...
- 前端不为人知的一面--前端冷知识集锦 前端已经被玩儿坏了!像console.log()可以向控制台输出图片
前端已经被玩儿坏了!像console.log()可以向控制台输出图片等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Quora上一个帖子,瞬间又GET了好多前 ...
- Activity 生命周期及其栈管理方式
Activity 生命周期 Android 系统用栈的形式管理 Activity , 当新的 Activity 被创建是, 会被放置到栈顶, 这个 Activity 会进入到运行状态, 而前一个 Ac ...
- 转:JQuery中$.ajax()方法参数详解
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- 去掉地址栏中的jsessionid
原来我在index.jsp中的编码是 <c:redirect url="/sys/login.shtm"/> 结果每次第一次登录都会在地址栏上出现了jsessionid ...
- Sqlserver 存储过程中结合事务的代码
Sqlserver 存储过程中结合事务的代码 --方式一 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ ...
- SQL Server事务的隔离级别
SQL Server事务的隔离级别 ########## 数据库中数据的一致性 ########## 针对并发事务出现的数据不一致性,提出了4个级别的解决方法: 隔离级别 第一类丢失更新 脏读 ...
- 原生js记住密码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- html标签,格式控制标签,内容容器标签,超链接标签,图片标签,表格
打开DREAMWEAVER,新建HTML,如下图: body的属性: bgcolor 页面背景色 background 背景壁纸.图片 text 文字颜色 topmargin 上边距 leftm ...
- handler的理解笔记
应用程序启动时,Android首先会开启一个主线程,如果此时需要一个耗时的操作,你不能把这些操作放在主线程中,如果你放在主线程中的话,界面会出现假死现象,这个时候我们需要把这些耗时的操作,放在一个子线 ...