- (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

转载于:https://www.cnblogs.com/lyz0925/p/5649866.html

UISearchController的使用相关推荐

  1. 苹果文档 UISearchController的介绍

    从这段介绍,可以知道这几个重点: 1.UISearchController 的 delegate 和 searchResultsUpdater 可以设置为其他控制器: 2.UISearchContro ...

  2. swift开发之--UISearchBar的使用/UISearchController的使用

    记录下UISearchBar的基本用法,补充:ios 8.0以后,原来的UISearchDisplayController被官方废弃,建议使用UISearchController,下面就简单的记录下这 ...

  3. iOS中的两种搜索方式UISearchDisplayController和UISearchController

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...

  4. iOS Swift UISearchController的取消按钮

    UISearchController的取消按钮 关于UISearchController的设置就不多说了,可以参考<UISearchController仿微信搜索框>或者自行上网查找. 情 ...

  5. iOS - UISearchController

    前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearch ...

  6. Swift UISearchController

    1.遵守协议 UISearchController 2.变量声明 var sc :UISearchController! 3.viewDidLoad中实现 sc = UISearchControlle ...

  7. tableview插入刷新_iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码...

    废话不多说了,直接给大家贴代码了. 具体代码如下所示: #import "ViewController.h" #import "TuanGouModel.h" ...

  8. iOS UISearchController和UITableView

    In this tutorial, we'll be developing an application that implements a search function over a TableV ...

  9. iOS——UISearchController(地图联想搜索Demo)

    简介 UISearchController是iOS 8 之后推出的一个新的控件, 用于创建搜索条, 及管理搜索事件, 使用这个, 我们可以很容易的创建属于自己的搜索框.搜索框在app开发中是非常常见的 ...

最新文章

  1. TAB(Nav)导航菜单
  2. python中的Lock与RLock
  3. composer 更新版本
  4. 傅立叶变换和小波变换入门学习
  5. Jenkins项目迁移
  6. 【Qt】创建线程程序示例
  7. linux装机量,在没有盗版的世界Linux桌面装机量占比40_-泡泡网
  8. 神奇的[Caller*]属性
  9. vim中的jk为什么是上下_JK的完整形式是什么?
  10. java 工具类sort_Java 通用排序工具类ListSortUtils
  11. 把本地的jar包打包到maven本地仓库里
  12. 7月共处理钓鱼网站1921个:非CN域名达1911 个
  13. Java的内存机制(转)
  14. FORM LOV特性
  15. AWK学习笔记八:AWK内置函数
  16. PCIe协议在具体实现时的大体框图
  17. Win10下 QT的安装配置 (亲测可用)
  18. 关于ucgui3.98(显示部分)移植
  19. 【Oracle11g官方文档阅读】DBA Essentials-上篇
  20. ps里面怎么插入流程图_Photoshop制作网站流程图详细过程

热门文章

  1. 有哪些必看的前端 JS 库?
  2. JSTL中的Core标签库
  3. python中delete怎么用_python删除np.deletePython基础教程05 - 缩进和选择
  4. python把print写入文件_Python中用print方法向文件中写入内容
  5. vba mysql 没有为命令对象设置命令_怎样解决“没有为命令对象设置命令”的错误? - .Net论坛 - 51CTO技术论坛_中国领先的IT技术社区...
  6. python 进程通信 延时_Python-----进程通信队列
  7. linux cp命令 强制覆盖,Linux中使用cp命令进行强制覆盖的方法
  8. 2020年日历电子版(打印版)_灵感 | 快来康康——2020年的日历设计的这么有创意?...
  9. FPGA学习---Quartus II新建工程
  10. 基于QItemDelegate的例子1 SpinBoxDelegate