搜索框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类型的值,当程序使用谓 ...
随机推荐
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- TYVJ P1013 找啊找啊找GF Label:动态规划
做题记录:2016-08-15 22:19:04 背景 MM七夕模拟赛 描述 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手,你是我的好GF.再见.""诶,别再见啊.. ...
- [shell] if else以及大于、小于、等于逻辑表达式 [转]
本文也即<Learning the bash Shell>3rd Edition的第五章Flow Control之读书笔记,但我们将不限于此.flow control是任何编程语言中很常用 ...
- 什么是SQL注入式攻击
什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响 ...
- LINUX 2.6.18-238 local root exp
/* * * * 1-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=0 * 0 _ __ __ __ 1 * ...
- TinyMCE在线编辑器使用方法初探
首先,下载TinyMCE包,地址:http://www.tinymce.com/ 然后将下载后的包解压,放置到一个文件夹下,创建一个html文件,并在其中书写如下代码: <!DOCTYPE ht ...
- MySQL 5.7在线设置复制过滤
很久没有更新博客了,主要是公司事情比较多,最近终于闲下来了.然而5.7也GA了,有许多新的特性,其中现在可以进行在线设置复制过滤了.但是还是得停复制,不过不用重启实例了.方便了DBA们进行临时性的调整 ...
- C# - Lee 公共类库
我的公共类库 using System; using System.IO; using System.Net; using System.Security.Cryptography; using Sy ...
- PHP Error 和 Logging 函数
PHP Error 和 Logging 函数 PHP Error 和 Logging 简介 Error 和 Logging 函数允许您对错误进行处理和记录. Error 函数允许用户定义错误处理规则, ...
- link them together by means of pointers
Computer Science An Overview _J. Glenn Brookshear _11th Edition An alternative to storing a heteroge ...