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.基本图形绘制* 线段(线宽. ...
随机推荐
- CDOJ 1287 MC挖矿世界(Spfa+set优化)
题目大意:原题链接 解题思路:此题要求多点最短距离,但是直接套用floyd会超时. 然后我们想直接从每一个点开始bfs就好了,但是还是会TLE,为什么呢? 因为你访问了很多次没有意义的地方,因为有些点 ...
- CSS3、SVG、Canvas、WebGL动画精选整理
一.CSS3动画 名称 用途 链接 阴影波纹特效 1.元素hover效果 2.突出表现效果 http://www.jq22.com/code80 横板导航菜单动画 导航菜单 http://www.jq ...
- iis日志时间与本地日期不一样
iis日志里面的时间是 UTC时间,所以要自已加时区进行转换:即 utc时间+8小时:
- jQuery中this 和 $(this)
var node = $('#id'); node.click(function(){ this.css('display','block'); //报错 this是一个html元素,不是jquer ...
- s3cmd安装
配置yum.repos cd /etc/yum.repos.d/ vim s3tools.repo [s3tools] name=Tools for managing Amazon S3 - Simp ...
- hashmap总结2
1. 关于HashMap的一些说法: a) HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体.HashMap的底层结构是一个数组,数组中的每一项是一条链表. b) Hash ...
- VMware 共享目录不显示的解决办法
centos mount -t vmhgfs .host:/ /mnt/hgfs # 如果有如下提示: # Error: cannot mount filesystem: No such device ...
- bzoj 3545: [ONTAK2010]Peaks
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1124 Solved: 304[Submit][Status][Discuss] Descripti ...
- Python学习笔记(十二)—Python3中pip包管理工具的安装【转】
本文转载自:https://blog.csdn.net/sinat_14849739/article/details/79101529 版权声明:本文为博主原创文章,未经博主允许不得转载. https ...
- springMVC数据回显
1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...