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. linux gdb 没有符号表被读取。请使用 "file" 命令。

    使用gdb时遇到这个问题,刚开始接触linux下使用gdb调试程序,其原因是生成的二进制可执行文件没有使用-g选项. gcc中-g选项是为了获得有关调试信息,要用gdb进行调试,必须使用-g生成二进制 ...

  2. 用C语言实现素数筛法获取一亿(100000000)以内的全部素数

    具体筛法是:先把n个自然数按次序排列起来.1不是质数,也不是合数,要划去.第二个数2是质数留下来,而把2后面所有能被2整除的数都划去.2后面第一个没划去的数是3,把3留下,再把3后面所有能被3整除的数 ...

  3. $_ 与 $PSItem

    PowerShell 3.0 中的$PSItem 此文章于2012年11月4日发表在PowershellPowershell小技巧并加以Powershell 3.0管道的标签 by Mooser Le ...

  4. PHP下利用PHPMailer配合QQ邮箱下的域名邮箱发送邮件(转)

    首先确定不是开启socks openssl phpinfo就可以知道 下载phpmailer   地址:https://github.com/PHPMailer/PHPMailer 下载完整, 个人和 ...

  5. Mininet实验 使用l2_multi模块寻找最短路径实验

    参考:使用l2_multi模块寻找最短路径实验 1. 实验目的 1.认识VND并且掌握其基本使用方法. 2.学会使用pox控制器的l2_multi模块寻找主机间的最短传输路径. 2. 实验原理 VND ...

  6. Thinkphp 不显示生成的验证码 【转载】

    在调用验证码之前加上 ob_clean(); 不显示验证码的代码: public function verify(){ $verify = new \Think\Verify(); $verify-& ...

  7. mysql查询所有字段(*),并且联表时需要特别注意的地方

    如果不标明*是读取哪个表,确实会将所有三个表都读出来.需要小心

  8. C#创建Excel

    创建Workbook说白了就是创建一个Excel文件,当然在NPOI中更准确的表示是在内存中创建一个Workbook对象流. 本节作为第2章的开篇章节,将做较为详细的讲解,以帮助NPOI的学习者更好的 ...

  9. Nginx 上的 php-fpm 资源侵占问题

    测试人员向我们反映:在Facebook平台的游戏比其它平台的游戏明显更慢.我询问,是不是因为FQ网络原因.他们说:不是,其它游戏也比较流畅.使用httpwatch查看了http请求,发现api.php ...

  10. C++ 中static 使用大全

    /// 静态全局变量 :只能在当前cpp中访问到  static int s_global = 0; void funcA() {      /// 静态局部变量 (函数静态变量) 初始化过一次就不会 ...