搜索框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类型的值,当程序使用谓 ...
随机推荐
- BZOJ3750 : [POI2015]Pieczęć
枚举第一个位置,然后暴力检验. #include<cstdio> #define N 1010 int T,n,m,a,b,x,y,i,j,k,q[N*N][2],cnt;char s[N ...
- BZOJ3547 : [ONTAK2010]Matchings
树形DP f[i][0]表示不向下连边的最大匹配数 f[i][1]表示向下连一条边的最大匹配数 h[][]表示对应的方案数 为了防止爆栈用BFS 为了防止MLE: 1.数组循环利用,比如存边的数组在存 ...
- BX2001: IE 支持使用 window.clipboardData 访问系统剪贴板,Chrome 和 Safari 中存在类似的 Clipboard 对象但尚未实现,Firefox 和 Opera 不支持这类对象
http://www.w3help.org/zh-cn/causes/BX2001 标准参考 无 问题描述 IE 支持使用 window.clipboardData 对象内的一系列方法访问系统剪贴板: ...
- TYVJ P1091 等差数列 Label:dp
背景 广东汕头聿怀初中 Train#3 Problem 3 描述 等差数列的定义是一个数列S,它满足了(S[i]-S[i-1]) = d (i>1).显然的一个单独的数字或者两个数字也可以形成一 ...
- HDU 4417 Super Mario(划分树+二分)
题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...
- 常用正则表达式(?i)忽略字母的大小写!
1.^/d+$ //匹配非负整数(正整数 + 0) 2.^[0-9]*[1-9][0-9]*$ //匹配正整数 3.^((-/d+)|(0+))$ //匹配非正整数(负整数 + 0) 4.^-[0-9 ...
- 转 c# 日期函数[string.Format----GetDateTimeFormats]格式 .
DateTime dt = DateTime.Now;Label1.Text = dt.ToString();//2005-11-5 13:21:25Label2.Text = dt.ToFileTi ...
- github配置
注册github账号: 准备秘钥文件: 认证: https://github.com 测试秘钥: 创建仓库: 执行下面命令创建git远程仓库: 添加一个two.txt文件:
- java 文件读写
http://blog.csdn.net/jiangxinyu/article/details/7885518
- linux的信号机制
软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核也可以因为内部事件而给进程发送信号,通知进程发生了某个事件.注意,信号只是用 ...