UISearchController的使用
- (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的使用的更多相关文章
- UISearchController 的用法[点击搜索框,自动到顶部]
//在ViewDidLoad里面如下代码 self.searchViewController = [[UISearchController alloc]initWithSearchResultsCon ...
- iOS UISearchController的使用
在iOS9中,UISearchDisplayController 已经被UISearchController替代.搜索框是一种常用的控件. 假设我们要满足下图的需求,产生100个“数字+三个随机字母” ...
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- UISearchController使用
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索 iOS8提供实现搜索功能的SDK:UISearchController(iOS8.0之后).UIS ...
- UItableview 添加 uisearchController
@property (nonatomic, strong) UISearchController* searchController; self.searchController = [[UISear ...
- UISearchController
搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UIS) 1.在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便 ...
- UITableView与UISearchController搜索及上拉加载,下拉刷新
#import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableVi ...
- iOS原生的搜索:UISearchController
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...
- 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 ...
- iOS8以后 UISearchController的用法
查了不少资料,都不太全,自己查看了apple文档,写了一份代码: 如下(只是界面): 1. 声明属性 @property (nonatomic, strong) UISearchController ...
随机推荐
- VS小技巧
1."清理解决方案":在对程序进行分发.上传时时常需要压缩解决方案文件夹,这时如果还嫌文件太大,可以在VS里右键解决方案---清理解决方.完成后,则该解决方案下的所有项目的将所有中 ...
- java JDK8 学习笔记——第15章 通用API
第十五章 通用API 15.1 日志 15.1.1 日志API简介 1.java.util.logging包提供了日志功能相关类与接口,不必额外配置日志组件,就可在标准Java平台使用是其好处.使用日 ...
- java实验一实验报告
Java实验报告一:Java开发环境的熟悉 ...
- jQuery.mobile.activePage获取当点活动的page
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- AQS 与 LockSupport
1.结构 Lock的实现类其实都是构建在AbstractQueuedSynchronizer上,每个Lock实现类都持有自己内部类Sync的实例 二.二元信号量 A semaphore initial ...
- bodyParser注意“需要请求头的支持”
bodyParser 支持此类参数解析. 注意: 在提交之前需要指定http 请求头为 content-type=application/json 代码如下: var express = requir ...
- C++ 编译器内存错误 after Normal block。。。
解决 after Normal block(#908) at 0x399EC0. CRT detected that the application wrote to memory after end ...
- ASP.NET MVC 利用ActionFilterAttribute来做权限等
ActionFilterAttribute是Action过滤类,该属于会在执行一个action之前先执行.而ActionFilterAttribute是 MVC的一个专门处理action过滤的类.基于 ...
- [LeetCode]题解(python):092 Reverse Linked List II
题目来源 https://leetcode.com/problems/reverse-linked-list-ii/ Reverse a linked list from position m to ...
- JS-007-富文本域操作
在日常 web 编写过程中,富文本域几乎成为了一个网站不可页面元素,同时,其也有着各种各样的实现方式,网络上也存在着各种各样的集成插件可供引用.此文以 js 获取.修改 163 邮箱写邮件时的邮件内容 ...