- (void)addSearchController

{

_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

_searchController.delegate = self;

_searchController.searchResultsUpdater = self;

// 必须要让searchBar自适应才会显示

[_searchController.searchBar sizeToFit];

_searchController.searchBar.delegate = self;

[_searchController.searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];

_searchController.searchBar.backgroundImage = [UIImage imageWithColor:kAppBakgroundColor];

_searchController.searchBar.placeholder = @"用户ID/昵称";

_searchController.hidesNavigationBarDuringPresentation = NO;

_searchController.dimsBackgroundDuringPresentation = NO;

self.definesPresentationContext = NO;

//把searchBar 作为 tableView的头视图

self.tableView.tableHeaderView = _searchController.searchBar;

}

//把searchBar 作为 tableView的头视图

self.tableView.tableHeaderView = _searchController.searchBar;

#pragma mark - UISearchController的代理

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

if (self.searchResults.count) {

[self.searchResults removeAllObjects];

}

NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"userId CONTAINS[c] %@",searchController.searchBar.text];

NSArray *array = [(NSArray *)self.searchArray filteredArrayUsingPredicate:searchPredicate];

self.searchResults = [NSMutableArray arrayWithArray:array];

//刷新表格

[self.tableView reloadData];

}

在tableView中使用时,可以根据_searchController.active的属性来判断是否显示搜索到的数据

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if (_searchController.active) {

return [self.searchResults count];

}else{

id<IMAContactDrawerShowAble> drawer = [_contact objectAtIndex:section];

if ([self isDrawerFolded:drawer])

{

return 0;

}

else

{

return [drawer items].count;

}

}

}

参考地址:http://www.mamicode.com/info-detail-1156195.html

http://www.2cto.com/kf/201510/447287.html

UISearchController的使用的更多相关文章

  1. UISearchController 的用法[点击搜索框,自动到顶部]

    //在ViewDidLoad里面如下代码 self.searchViewController = [[UISearchController alloc]initWithSearchResultsCon ...

  2. iOS UISearchController的使用

    在iOS9中,UISearchDisplayController 已经被UISearchController替代.搜索框是一种常用的控件. 假设我们要满足下图的需求,产生100个“数字+三个随机字母” ...

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

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

  4. UISearchController使用

    iOS8之前我们使用UISearchDisplayController做TableView的本地搜索 iOS8提供实现搜索功能的SDK:UISearchController(iOS8.0之后).UIS ...

  5. UItableview 添加 uisearchController

    @property (nonatomic, strong) UISearchController* searchController; self.searchController = [[UISear ...

  6. UISearchController

    搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UIS) 1.在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便 ...

  7. UITableView与UISearchController搜索及上拉加载,下拉刷新

    #import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableVi ...

  8. iOS原生的搜索:UISearchController

    iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...

  9. UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

    Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...

  10. iOS8以后 UISearchController的用法

    查了不少资料,都不太全,自己查看了apple文档,写了一份代码: 如下(只是界面): 1. 声明属性 @property (nonatomic, strong) UISearchController ...

随机推荐

  1. 用c++builder读取一个一行有多行变量的文件

    文件内容如下: C DXDY.INP FILE, IN FREE FORMAT ACROSS COLUMNS for  83658 Active CellsC 2013-5-25   上午 10:43 ...

  2. mysql ERROR 1045 (28000): Access denied for user解决方法 (转)

    问题重现(以下讨论范围仅限Windows环境): C:\AppServ\MySQL> mysql -u root -pEnter password:ERROR 1045 (28000): Acc ...

  3. 抓包工具Charles 【转】

      今天就来看一下Mac上如何进行抓包,之前有一篇文章介绍了使用Fidder进行抓包 http://blog.csdn.net/jiangwei0910410003/article/details/1 ...

  4. php读取qqwry.dat ip地址定位文件的类

    <?php// +----------------------------------------------------------------------// |// +---------- ...

  5. 详解 Qt 线程间共享数据(用信号槽方式)

    使用共享内存.即使用一个两个线程都能够共享的变量(如全局变量),这样两个线程都能够访问和修改该变量,从而达到共享数据的目的. Qt 线程间共享数据是本文介绍的内容,多的不说,先来啃内容.Qt线程间共享 ...

  6. c# ToString() 用法

    string tempa = Convert.ToString(31, 2);//将10进制数31转换为2进制字符串. string strNums = int.Parse(tempa).ToStri ...

  7. 【Java 基础篇】【第九课】继承

    继承就是为了提高代码的复用率. 利用继承,我们可以避免代码的重复.让Woman类继承自Human类,Woman类就自动拥有了Human类中所有public成员的功能.我们用extends关键字表示继承 ...

  8. npm的使用

    npm的用途: 用官方的包 用其他人的包 传自己的包给别人用 一: 安装 1.用别人的: npm install argv 这样下载好之后,argv包就放在了工程目录下的node_modules目录中 ...

  9. Invitation Cards---poj1511(spfa)

    题目链接:http://poj.org/problem?id=1511 有向图有n个点m条边,求点1到其他n-1个点的最短距离和+其他点到点1的最小距离和: 和poj3268一样,但是本题的数据范围较 ...

  10. windows下安装yaf和git

    不得不说win7下安装yaf比mac下安装yaf简单多了 1. phpinof()看一下你的php版本.我的是php 5.4所以我选择是php_yaf-2.1.9-x86-5.4-zts-nodebu ...