- (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. 各大网站CSS代码初始化集合

    css代码之所以初始化,是因为能尽量减少 各浏览器之间的兼容性问题! 腾讯QQ官网 样式初始化 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fiel ...

  2. trunc sysdate

    select *  from per_all_people_f papf where trunc(sysdate) between trunc(papf.effective_start_date) a ...

  3. css中IE6fix问题

    .g-popup-mask, .g-popup-box { position:fixed; top:0; left:0; z-index:10000; _position:absolute; _top ...

  4. JVM学习笔记

    1. JVM中的直接引用和符号引用 JVM在装载class文件的时候,会有一步是将符号引用解析为直接引用的过程. 那么这里的直接引用到底是什么呢? 对于指向“类型”[Class对象].类变量.类方法的 ...

  5. 树莓派如何便捷的使用pi4j

    问题的由来 pi4j用起来很方便,但是感觉pi4j库的命名太杂乱,啰嗦了,很容易弄混,而且好像没听说官方有自己的编译器.如果没有智能点的编辑器的话,写起来真要命,但是树莓派运行Eclipse不太现实, ...

  6. 20145211 《Java程序设计》实验报告五————Java网络编程及安全实验报告

    实验内容 1.掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验步骤 这一部分是与我的partner合作的,详见他的博客- [20145326 <Java程序设计> ...

  7. 单个未知大小图片在div里面垂直居中的方法。。。添加辅助元素挤一下位置达到居中

    单个未知大小图片在div里面垂直居中的方法...添加辅助元素挤一下位置达到居中   <div class="ServicesLiTopPic"> <i>&l ...

  8. NuGet的几个小技巧

    因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...

  9. ASP.NET MVC3 Areas 分离项目 同名控制器(同名Controller) 演示demo

    为什么需要分离? 我们知道MVC项目各部分职责比较清晰,相比较ASP.NET Webform而言,MVC项目的业务逻辑和页面展现较好地分离开来,这样的做法有许多优点,比如可测试,易扩展等等.但是在实际 ...

  10. .NET:OrderBy和ThenBy

    .NET中OrderBy和ThenBy的语义是不同的,如:list.OrderBy(x=>x.A).OrderBy(x=>x.B),那么最终只会根据B进行排序:list.OrderBy(x ...