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. POJ 2217 (后缀数组+最长公共子串)

    题目链接: http://poj.org/problem?id=2217 题目大意: 求两个串的最长公共子串,注意子串是连续的,而子序列可以不连续. 解题思路: 后缀数组解法是这类问题的模板解法. 对 ...

  2. TYVJ P1067 合唱队形 Label:上升子序列?

    背景 NOIP2004 提高组 第三道 描述     N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号 ...

  3. 【BZOJ】1043: [HAOI2008]下落的圆盘(计算几何基础+贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1043 唯一让我不会的就是怎么求圆的周长并QAAQ... 然后发现好神!我们可以将圆弧变成$[0, 2 ...

  4. Eclipse开发JavaWeb程序报Server Tomcat v7.0 at localhost was unable to start

    出处:http://www.javaweb1024.com/info/582.jspx 原因重现: Eclipse开发JavaWeb程序,启动Servers的Tomcat服务器,突然跳出弹出框,内容显 ...

  5. HttpClient_HttpClient 对 cookie的处理

    session的保持是通过cookie来维持的,所以如果用户有勾选X天内免登录,这个session 就X天内一直有效,就是通过这个cookie来维护.如果没选X天内免登录,基本上就本次才能保持sess ...

  6. Leetcode | N-Queens I & II

    N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...

  7. CSS3:背景

    细节注意 盒子:border-box | padding-box | content-box用于background-origin背景的开始和background-clip的剪切位置. 渐变也是生成一 ...

  8. mysql insert一条记录后怎样返回创建记录的主键id,last_insert_id(),selectkey

    mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...

  9. python多线程使用

    内容链接: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00138683236 ...

  10. ExpandableListView实现子Item的点击事件

    在继承的BaseExpandableListAdapter的ExpandableListView的Adapter中,重写以下方法 @Override public boolean isChildSel ...