IOS UI-UISearchController
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的更多相关文章
- [IOS]IOS UI指南
[IOS]IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻 ...
- IOS UI 第八篇:基本UI
实现图片的滚动,并且自动停止在每张图片上 - (void)viewDidLoad{ [super viewDidLoad]; UIScrollView *scrollView = [[U ...
- 国外IOS UI指南
国外IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻译) ...
- iOS UI的几种模式
iOS UI的几种模式: 1.平凡模式(原生控件组合): 2.新闻模式: 3.播放器模式: 4.微博模式:
- 通过实现一个TableView来理解iOS UI编程
推荐一篇神作: 通过实现一个TableView来理解iOS UI编程 http://blog.jobbole.com/61101/
- iOS中 UISearchController 搜索栏 UI技术分享
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...
- [iOS UI设计笔记整理汇总]
8.UIsearchbar放到Navigationbar 上(意思是建个View作为titleview) //此处调用的是第三方封装的SearchBar,也可以自定义. self.searchBarW ...
- iOS UI高级之网络编程(HTTP协议)
HTTP协议的概念 HTTP协议,Hyper Text Transfer Protocol (超文本传输协议)是用于从万维网服务器传送超文本到本地浏览器的传输协议,HTTP是一个应用层协议,由请求和响 ...
- iOS - UI - UIWebView
1.UIWebView UIWebView 是 苹果提供的用来展示网页的UI控件.它也是最占内存的控件. iOS8.0 webkit框架. WKWebView,相比UIWebView,节省了1/3~1 ...
- [iOS UI进阶 - 0] Quiartz2D
A.简介 1. 需要掌握的 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 1.基本图形绘制* 线段(线宽. ...
随机推荐
- JVM内存—堆(heap)栈(stack)方法区(method) (转)
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 堆区:1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令 ...
- Python的幂运算
直接用例子说明
- Spring 事务管理tx,aop
spring tx:advice事务配置 2016年12月21日 17:27:22 阅读数:7629 http://www.cnblogs.com/rushoooooo/archive/2011/08 ...
- Web前端页面的浏览器兼容性测试心得(一)搭建测试用本地静态服务器
自从实习以来,我体验了由一个人编程再到多人协作编程的转换.同时也接触了很多新鲜事物,学到了很多东西.工作时,常常需要多个人协同测试一张H5页面(PC.手机.平板等跨设备测试),也会经常把页面给PM或老 ...
- java中内部类的积累
放在一个类的内部的类我们就叫内部类. 二. 作用 1.内部类可以很好的实现隐藏 一般的非内部类,是不允许有 private 与protected权限的,但内部类可以 2.内部类拥有外围类的所有元素的访 ...
- ISO8583
最开始时,金融系统只有IBM这些大的公司来提供设备,象各种主机与终端等.在各个计算机设备之间,需要交换数据.我们知道数据是通过网络来传送的,而在网络上传送的数据都是基于0或1这样的二进制数据,如果没有 ...
- ONVIF学习-ONVIF开发框架搭建(C++)
第一步.下载gsoap 从gsoap官网(http://www.genivia.com/products.html#notice)下载最新版gsoap(博主用的是gsoap_2.8.45).gsoap ...
- Swoole学习(二)Swoole之TCP服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...
- xml简单介绍及libmxml编程
今天我们来简单介绍一下,关于xml的一些内容,包括自己编写一些程序进行生成和解析. 首先我们我们还是从xml的演化历史来了解一下它吧. 历史演化 GML: 在20世纪60年代为了促进数据交换和操作,通 ...
- shell编程学习笔记之sed编辑器
在shell编程中,大多数处理的都是文本文件.对文本文件进行处理除了使用交互式文本编辑器(vi[m],gedit......)也可以使用另外一类:流编辑器. 流编辑器:使用预定义的编辑规则来对文本进行 ...