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. 工作笔记——区块链POC

    1.基础配置 安装SecureCRT 8.0链接到虚拟服务器,并配置docker 安装文件上传到服务器工具FileZilla

  2. 查看电脑已经连过的wifi密码

    用下面两条命令可以完成 查看当前系统已经保存的网络 netsh wlan show profiles 查看wifi指定密码 netsh wlan show profiles name="wi ...

  3. Linux系统——Raid磁盘阵列

    Raid磁盘阵列 作用:解决磁盘速度.安全问题 Raid原理 Raid0 写入速度极快,有几块硬盘,写入速度就近似几倍,但是安全性极差,只要一块盘坏了,所有盘的数据全部坏掉,最少两块硬盘组合 性价比最 ...

  4. Linux系统——本地yum仓库安装

    一.yum仓库概述 yum是基于rpm包管理,能够从指定的服务器自动下载rpm包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无需繁琐地一次次下载.安装. 二.yum仓库安装的方式 ...

  5. ACM对拍程序

    1.把所需对拍的代码的可执行文件a.exe b.exe放在同一目录下 2.把rand数据的代码的可执行文件c.exe放在该目录下 3.新建一个txt文件,里面添加代码,后把格式改成bat @echo ...

  6. win32api win32gui win32con 窗口句柄 发送消息 常用方法

    Pywin32是一个Python库,为python提供访问Windows API的扩展,提供了齐全的windows常量.接口.线程以及COM机制等等. 1.通过类名和标题查找窗口句柄,并获得窗口位置和 ...

  7. 算法+OpenCV】基于opencv的直线和曲线拟合与绘制(最小二乘法)

    http://blog.csdn.net/guduruyu/article/details/72866144 最小二乘法多项式曲线拟合,是常见的曲线拟合方法,有着广泛的应用,这里在借鉴最小二乘多项式曲 ...

  8. jquery获取select选中项的文本

    使用jquery获取选中的值很简单 $("#select").val(); 但是获取选中的文本就没有这么直接了 $("#select").find(" ...

  9. 关于Linq翻译Inner join ,Left join (本文为转载)

    我們先來一段最基本的 LINQ to SQL 使用類似 T-SQL 的 INNER JOIN 資料查詢語法: from c in Categories from o in c.Products sel ...

  10. 2045331 《Java程序设计》第09周学习总结

    2045331 <Java程序设计>第09周学习总结 教材学习内容总结 第十六章 整合数据库 16.1.1JDBC简介 1.JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接 ...