- (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. iOS开发入门教程

    iOS开发入门教程 http://my.oschina.net/mailzwj/blog/133273 摘要 iOS开发入门教程,从创建项目到运行项目,包括OC基础,调试,模拟器设置等相关知识. iO ...

  2. 纯C++文件调用MFC类

    在VS2008中 将预编译头属性 由 不使用预编译头 改成 使用使用预编译头 在响应的.cpp文件的最前面 #include "stdafx.h"

  3. ::after,::before使用

    ::after,::before使用   1.:before 选择器在被选元素的内容前面插入内容. 请使用 content 属性来指定要插入的内容. <!DOCTYPE html> < ...

  4. POJ 1236 Network of Schools (Tarjan + 缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 48 ...

  5. Archiver 浅析

    归档是一个过程,即用某种格式来保存一个或多个对象,以便以后还原这些对象.通常,这个过程包括将(多个)对象写入文件中,以便以后读取该对象. 两种归档数据的方法:属性列表和带键值的编码. 属性列表局限性很 ...

  6. SqlServer数据组织结构

    page页 每个页面8KB,连续的8个页面称之为一个区extents, 如:2.18MB的一个DB的区大约有 2.18 MB (2,293,760 字节)=2,293,760b/8kb=280个页面= ...

  7. AutoLayout技术选型和应用

    前言:这篇文章是笔者在项目中对布局技术进行技术选型和应用的相关介绍,供大家参考. && [self.buttonscount] > 0) { UIButton *button = ...

  8. qt cef嵌入web

    原文http://blog.sina.com.cn/s/blog_9e59cf590102vnfc.html 最近项目需要,研究了下libcef库. Cef(Chromium Embedded Fra ...

  9. 【Android测试】【第三节】ADB——源码浅谈

    ◆版权声明:本文出自carter_dream的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4651724.html 前言 由于本人精力 ...

  10. Python 扫面文件夹中的文件

    #coding=utf-8 import os,sys reload(sys) sys.setdefaultencoding("utf-8") def scan_files_sub ...