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)的更多相关文章

  1. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  2. iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  3. iOS 搜索框控件 最简单的dome

    刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...

  4. iOS8之后搜索框的常规实例

    1.在一个二级导航控制器中添加一个UITableviewController作为子控制器 2.UITableviewController.tableView 作为展示结果 3.利用iOS之后的UISe ...

  5. iOS 新浪微博-2.0 搜索框/标题带箭头/下拉菜单

    不管是搜索框还是下拉菜单,我们都需要对背景的图片进行拉伸.定义一个Category分类对图片进行操作. UIImage+Effect.h #import <UIKit/UIKit.h> @ ...

  6. 使用 Vue.js 2.0+ Vue-resource 模仿百度搜索框

    使用 Vue.js 2.0 模仿百度搜索框 <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...

  7. 搜索框(Thinkphp5.0)

    1.普通关键词搜索框 模板部分代码: <form name='searchform' action='/index.php/module/controller/search' method='g ...

  8. iOS搜索框

    在iOS8以前搜索框是作为一个控件添加到TableViewController中, 有系统自带的搜索变量self.searchDisplayController 遵守一个搜索显示的协议<UISe ...

  9. iOS学习之NSPredictae及搜索框的实现

    NSPredicate Predicate 即谓词逻辑, Cocoa框架中的NSPredicate用于查询,作用是从数据堆中根据条件进行筛选.计算谓词之后返回的结果永远为BOOL类型的值,当程序使用谓 ...

随机推荐

  1. android sdk 安装排错

    如果你遇到了消息为“Failed to fetch URL…” 的错误提示,那么你需要将HTTPS方式改为HTTP方式,方法如下: 碰到这样错误,请按下边的操作. 1)在菜单选择Tools—Optio ...

  2. ubuntu下新建用户的终端不显示当前路径,不能用上下光标键得到使用过的命名解决办法

    这几天我装ubuntu10.10,xubuntu12.04创建新用户的时候,总会遇到这个问题 就是打开终端的时候,没有路径了,即:xxx@xxx:~$ 找了很久,最后找到了(http://www.os ...

  3. POJ 3691 DNA repair(AC自动机+DP)

    题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...

  4. MySQL配置SQL Assistant提示

    以前开发一直使用SQL Server数据库,提示插件采用的就是SQL Assistant,写起脚本来相当有效率.这段时间公司转型要采用MySQL数据库,试用了mysql workbench.mysql ...

  5. Toll-Free Bridge

    引 在深入了解桥接机制的时候看到一篇好文,虽然已经很久远,但是忍不住看了好几遍,心中诸多不解一扫而光.在此放上链接: 原文:http://ridiculousfish.com/blog/posts/b ...

  6. JS让input按钮不能点击

    <input value="开通" type="button" id="tijiao" class="button" ...

  7. 使用javabean连接数据库时遇到的问题

    1.whitespace问题.是便签了每空格如: 就会出现下面的问题.contentType前每空格.空格后就解决了 2. 这是部署问题,关闭,重新部署 追问 怎么部署? 回答 1.选中项目 F5(e ...

  8. [转]支付宝接口程序、文档及解读(ASP.NET)

    本文转自:http://www.cnblogs.com/blodfox777/archive/2009/11/03/1595223.html 最近需要为网站加入支付宝的充值接口,而目前关于支付宝接口开 ...

  9. [kuangbin带你飞]专题八 生成树 - 次小生成树部分

    百度了好多自学到了次小生成树 理解后其实也很简单 求最小生成树的办法目前遇到了两种 1 prim 记录下两点之间连线中的最长段 F[i][k] 之后枚举两点 若两点之间存在没有在最小生成树中的边 那么 ...

  10. VIM常用快捷键~网页上查找

    转自~木枫林 转自~鸟哥的私房菜 第十章.vim 程序编辑器 第十章.vim 程序编辑器 最近更新日期:2009/08/20 2. vi 的使用 2.1 简易执行范例 2.2 按键说明 2.3 一个案 ...