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. This function has none of DETERMINISTIC, NO SQL 解决办法

    在恢复mysql数据库时提示 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its decl ...

  2. CC150 - 11.2

    Question: Write a method to sort an array of strings so that all the anagrams are next to each other ...

  3. COJ559 回文

    试题描述 给定字符串,求它的回文子序列个数.回文子序列反转字符顺序后仍然与原序列相同.例如字符串aba中,回文子序列为"a", "a", "aa&qu ...

  4. Linux压力测试工具

    1 http_load:http://www.oschina.net/p/http_load 命令行输入man http_load 或者 http_load -h可以看到工具的使用方式: 参数说明: ...

  5. c++ namespace的使用

    ** namespace:命名空间就是为解决C++中的变量.函数的命名冲突而服务的. ** namespace定义的格式基本格式是: namespace identifier {    entitie ...

  6. HDU 1176 经典dp

    记录最晚时间 从time为2枚举到最晚时间 每个时间段的x轴节点都等于上一个时间段的可触及的最大馅饼数 #include<stdio.h> #include<string.h> ...

  7. [IT新应用]如何部署CITRIX 虚拟桌面

    1.搭建AD,并部署dhcp. 2.安装ddc服务器,加入域.这台服务器就是用来发布后端服务器或者WIN7的PC给用户使用.相当于调度. 3.安装windows server或者win7,用于发布给用 ...

  8. sql order by+字段,指定按照哪个字段来排序

    1.我们就可以使用 MySQL 的 ORDER BY 子句来设定你想按哪个字段哪中方式来进行排序,再返回搜索结果. 2.SELECT field1, field2,...fieldN table_na ...

  9. 翻译 Tri-Ace:在Shader里近似渲染公式

    继上一篇:次世代基于物理渲染的反射模型,本篇是Tri-Ace 在cedec2014上最近发布的, 主要内容如名称所示,解释了他们在实现基于物理渲染时,对shader的渲染公式所做的近似工作.     ...

  10. ThinkPHP公共配置文件与各自项目中配置文件组合的方法

    ThinkPHP公共配置文件与各自项目中配置文件组合的方法 文章TAG:thinkphp 公共配置文件 时间:2014-11-25来源:www.aspku.com 作者:源码库 文章热度: 146 ℃ ...