ViewController.m
 //
// ViewController.m
// IOS_0224_查找联系人
//
// Created by ma c on 16/2/24.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import "SearchResultsVC.h" @interface ViewController ()<UITableViewDataSource> @property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *keys;
@property (nonatomic, strong) NSDictionary *dict; @property (nonatomic, strong) UISearchController *searchCtr; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self createUI]; } - (NSArray *)keys
{
if (_keys == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
self.dict = [NSDictionary dictionaryWithContentsOfFile:path];
_keys = [[self.dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
}
return _keys;
} - (NSDictionary *)dict
{
if (_dict == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
self.dict = [NSDictionary dictionaryWithContentsOfFile:path];
}
return _dict;
} - (void)createUI
{
[self setupTableView];
[self setupSearchVC];
} - (void)setupTableView
{
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
} - (void)setupSearchVC
{
SearchResultsVC *resultVC = [[SearchResultsVC alloc] initWithDict:self.dict keys:self.keys];
self.searchCtr = [[UISearchController alloc] initWithSearchResultsController:resultVC]; self.searchCtr.searchResultsUpdater = resultVC; self.searchCtr.searchBar.placeholder = @"请输入搜索内容";
[self.searchCtr.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchCtr.searchBar;
} #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.keys.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = self.keys[section];
NSArray *value = self.dict[key]; return [value count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
NSString *key = self.keys[indexPath.section];
NSArray *value = self.dict[key]; cell.textLabel.text = value[indexPath.row];
return cell;
} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.keys[section];
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.keys;
} @end
SearchResultsVC.m
 //
// SearchResultsVC.m
// IOS_0224_查找联系人
//
// Created by ma c on 16/2/24.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "SearchResultsVC.h" @interface SearchResultsVC () @property (strong, nonatomic) NSDictionary *dict;
@property (strong, nonatomic) NSArray *keys;
@property (strong, nonatomic) NSMutableArray *searchList; @end @implementation SearchResultsVC - (instancetype)initWithDict:(NSDictionary *)dict keys:(NSArray *)keys {
if (self = [super initWithStyle:UITableViewStylePlain]) {
self.dict = dict;
self.keys = keys;
self.searchList = [[NSMutableArray alloc] init];
}
return self;
} - (void)viewDidLoad {
[super viewDidLoad];
} - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = [searchController.searchBar text];
NSLog(@"searchString:%@",searchString);
[self.searchList removeAllObjects]; if (searchString.length > ) { // NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",searchString];
NSPredicate *predicate =
[NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) { NSRange range = [evaluatedObject rangeOfString:searchString
options:NSCaseInsensitiveSearch]; return range.location != NSNotFound;
}]; for (NSString *key in self.keys) {
NSArray *matches = [self.dict[key]
filteredArrayUsingPredicate: predicate];
[self.searchList addObjectsFromArray:matches];
}
[self.tableView reloadData];
}
} #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.searchList count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.textLabel.text = self.searchList[indexPath.row];
return cell;
}
@end

IOS UI-UISearchController的更多相关文章

  1. [IOS]IOS UI指南

    [IOS]IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻 ...

  2. IOS UI 第八篇:基本UI

    实现图片的滚动,并且自动停止在每张图片上     - (void)viewDidLoad{    [super viewDidLoad]; UIScrollView *scrollView = [[U ...

  3. 国外IOS UI指南

    国外IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻译) ...

  4. iOS UI的几种模式

    iOS UI的几种模式: 1.平凡模式(原生控件组合): 2.新闻模式: 3.播放器模式: 4.微博模式:

  5. 通过实现一个TableView来理解iOS UI编程

    推荐一篇神作: 通过实现一个TableView来理解iOS UI编程 http://blog.jobbole.com/61101/

  6. iOS中 UISearchController 搜索栏 UI技术分享

    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...

  7. [iOS UI设计笔记整理汇总]

    8.UIsearchbar放到Navigationbar 上(意思是建个View作为titleview) //此处调用的是第三方封装的SearchBar,也可以自定义. self.searchBarW ...

  8. iOS UI高级之网络编程(HTTP协议)

    HTTP协议的概念 HTTP协议,Hyper Text Transfer Protocol (超文本传输协议)是用于从万维网服务器传送超文本到本地浏览器的传输协议,HTTP是一个应用层协议,由请求和响 ...

  9. iOS - UI - UIWebView

    1.UIWebView UIWebView 是 苹果提供的用来展示网页的UI控件.它也是最占内存的控件. iOS8.0 webkit框架. WKWebView,相比UIWebView,节省了1/3~1 ...

  10. [iOS UI进阶 - 0] Quiartz2D

    A.简介 1. 需要掌握的 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 1.基本图形绘制* 线段(线宽. ...

随机推荐

  1. SQL Server 自定义函数(Function)——参数默认值

    sql server 自定义函数分为三种类型:标量函数(Scalar Function).内嵌表值函数(Inline Function).多声明表值函数(Multi-Statement Functio ...

  2. CCF 交通规划(Dijkstra+优先队列)

    交通规划 问题描述 G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统. 建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成 ...

  3. CCF 炉石传说(模拟)

    试题编号: 201612-3 试题名称: 炉石传说 时间限制: 1.0s 内存限制: 256.0MB 问题描述 <炉石传说:魔兽英雄传>(Hearthstone: Heroes of Wa ...

  4. 持续(集成-->交付-->部署)

    软件的开发工作的大致流程 编码 -> 构建 -> 集成 -> 测试 -> 交付 -> 部署 由上图可知「持续集成(Continuous Integration)」.「持续 ...

  5. 《阿里巴巴Java开发规约》插件使用

    通过Jetbrains官方仓库安装 1. 打开 Settings >> Plugins >> Browse repositories... 2. 在搜索框输入alibaba即可 ...

  6. vue2.0中配置文件路径

    在build/webpack.base.conf.js中添加一些代码即可 module.exports = { resolve: { extensions: ['.js', '.vue', '.jso ...

  7. logstash编写2以及结合kibana使用

    有时候根据日志的内容,可能一行不能全部显示,会延续在下一行,为了将上下内容关联在一起,于是codec插件中的multiline插件 就派上用场了,源日志内容: [2017-09-20T16:04:34 ...

  8. Qt、MVB

    使用的陪测网卡是[众志诚MVB网卡],通过串口配置,可以接收和发送MVB数据. 一.MVB通信介绍 对于某一节点[如众志诚MVB网卡],为其配置0x710和0x720端口为源端口,用于发送数据,为其配 ...

  9. 如何优雅的进入IT世界

    原始地址:http://www.cocoachina.com/gamedev/misc/2014/0613/8808.html 以下三段是废话,请跳过.   (废话段1)有文科生想了解IT世界.这并不 ...

  10. 解题报告:codeforce 7C Line

    codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...