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. python中is和==区别

    is比较两个对象的id值是否相等,是否指向同一个内存地址 ==比较的是两个对象的内容是否相等,值是否相等 is运算符比==效率高,在变量和None进行比较时,应该使用is

  2. PKU 2823 Sliding Window(线段树||RMQ||单调队列)

    题目大意:原题链接(定长区间求最值) 给定长为n的数组,求出每k个数之间的最小/大值. 解法一:线段树 segtree节点存储区间的最小/大值 Query_min(int p,int l,int r, ...

  3. 部署apollo-client到maven私服上时遇到的问题及排查过程

    场景回顾: 应用客户端如果需要接入到Apollo配置服务中心的话,需要引用apollo-client的依赖包使之与config-server保持连接,从而可以及时的收到更新之后的配置信息. 1.将ap ...

  4. ABP官方文档翻译 1.3 模块系统

    ABP模块系统 介绍 模块定义 生命周期方法 PreInitialize Initialize PostInitialize Shutdown 模块依赖 插件系统 ASP.NET Core ASP.N ...

  5. Let Her Go

    转自:https://www.zhihu.com/question/29255072/answer/43962611 [MV]Passenger-Let Her Go-高清MV在线播放 版本一 Let ...

  6. LINUX SHELL 笔记 02: 变量初识

    https://www.shellscript.sh/variables1.html 变量是一个可操作(读.写)的内存块的名字. 尝试-1 创建一个变量: root@iZwz:~/labs# sh m ...

  7. Activiti工作流引擎数据库表结构

    Activiti工作流引擎数据库表结构 一.数据库表的命名 Acitiviti数据库中表的命名都是以ACT_开头的.第二部分是一个两个字符用例表的标识.此用例大体与服务API是匹配的. ACT_RE_ ...

  8. 《Java程序设计》实验3

    20145318 <Java程序设计>实验3 实验内容 使用 git 上传代码 使用 git 相互更改代码 实现代码的重载 PSP 队友链接 http://www.cnblogs.com/ ...

  9. 1.1_Django简介及安装

    Django的安装 Django安装 文档:https://docs.djangoproject.com/en/1.8/ pip install django 可以到这个网站查看可用的django版本 ...

  10. MysQL使用一高级应用(下)

    连接查询 连接查询分类如下: 表A inner join 表B:表A与表B匹配的行会出现在结果中 表A left join 表B:表A与表B匹配的行会出现在结果中,外加表A中独有的数据,未对应的数据使 ...