iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISearchDisplayController的组合方式.
添加UISearchController属性:
@property(strong, nonatomic) UISearchController *searchController;
@property(strong, nonatomic) NSMutableArray *allCities; // 所有城市
@property(strong, nonatomic) NSMutableArray *filteredCities; // 根据searchController搜索的城市
UISearchController初始化
在viewDidLoad中初始化UISearchController:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = false;
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.backgroundColor = UIColorFromHex(0xdcdcdc);
self.tableView.tableHeaderView = self.searchController.searchBar;
UISearchResultsUpdating协议
使用UISearchController要继承UISearchResultsUpdating协议, 实现其中的UISearchResultsUpdating方法.
#pragma mark - searchController delegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self.filteredCities removeAllObjects];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", self.searchController.searchBar.text];
self.filteredCities = [[self.allCities filteredArrayUsingPredicate:searchPredicate] mutableCopy];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
UISearchController的searchBar中的内容一旦发生变化, 就会调用该方法. 在其中, 我们可以使用NSPredicate来设置搜索过滤的条件.
更新UITableView
引入UISearchController之后, UITableView的内容也要做相应地变动: 即cell中要呈现的内容是allCities, 还是filteredCities.
这一点, 可以通过UISearchController的active属性来判断, 即判断输入框是否处于active状态.
UITableView相关的很多方法都要根据active来做判断:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (!self.searchController.active) {
return self.cityKeys.count;
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!self.searchController.active) {
NSString *key = self.cityKeys[section];
NSArray *citySection = self.cityDict[key];
return citySection.count;
} else {
return self.filteredCities.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// 根据UISearchController的active属性来判断cell中的内容
if (!self.searchController.active) {
NSString *key = self.cityKeys[indexPath.section];
cell.textLabel.text = [self.cityDict[key] objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = self.filteredCities[indexPath.row];
}
return cell;
}
UISearchController的移除
在viewWillDisappear中要将UISearchController移除, 否则切换到下一个View中, 搜索框仍然会有短暂的存在.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.searchController.active) {
self.searchController.active = NO;
[self.searchController.searchBar removeFromSuperview];
}
}
iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)的更多相关文章
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
1.searchResultsUpdater:设置显示搜索结果的控制器 ? 1 _mySearchController.searchResultsUpdater = self; 2.dimsB ...
- Android 自定义EditText实现类iOS风格搜索框
最近在项目中有使用到搜索框的地方,由于其样式要求与iOS的UISearchBar的风格一致.默认情况下,搜索图标和文字是居中的,在获取焦点的时候,图标和文字左移.但是在Android是并没有这样的控件 ...
- iOS 搜索框控件 最简单的dome
刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...
- iOS搜索框
在iOS8以前搜索框是作为一个控件添加到TableViewController中, 有系统自带的搜索变量self.searchDisplayController 遵守一个搜索显示的协议<UISe ...
- iOS8之后搜索框的常规实例
1.在一个二级导航控制器中添加一个UITableviewController作为子控制器 2.UITableviewController.tableView 作为展示结果 3.利用iOS之后的UISe ...
- iOS学习之NSPredictae及搜索框的实现
NSPredicate Predicate 即谓词逻辑, Cocoa框架中的NSPredicate用于查询,作用是从数据堆中根据条件进行筛选.计算谓词之后返回的结果永远为BOOL类型的值,当程序使用谓 ...
- iOS开发——UI篇OC篇&TextField作为搜索框的使用
TextField作为搜索框的使用 在iOS开发中我们经常会使用到搜索框,但是有的时候系统自带的搜索框不足以满足我吗想要的功能,这个时候我们就可以使用自定义的搜索框实现想要的功能. 今天就简单的介绍一 ...
- 【好程序员笔记分享】——iOS开发之使用TextField作为搜索框
-iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之使用TextField作为搜索框 今天给大家带来一个新的技巧,比如平时我们要使用代码创建一 ...
随机推荐
- 【转】C# 的Brush 及相关颜色的操作
// (实心刷) Rectangle rect1 = , , , ); SolidBrush sbrush1 = new SolidBrush(Color.DarkOrchid); SolidBrus ...
- MVC5 网站开发实践 2.1、管理员登陆
目录 MVC5 网站开发实践 概述 MVC5 网站开发实践 1.建立项目 MVC5 网站开发实践 2.后台管理 1. 创建SHA256加密方法. 在Data项目中添加文件夹[Security ...
- 前端学PHP之运算符
× 目录 [1]总括 [2]算术运算符 [3]赋值运算符[4]位运算符[5]比较运算符[6]错误控制[7]逻辑运算符[8]字符串连接[9]数组运算符 前面的话 运算符是可以通过给出的一或多个表达式来产 ...
- C#:Md5和Sha1两种加密方式
1.新建控制台应用程序 2.新建类 EncryptHelper.cs public static class EncryptHelper { /// <summary> /// 基于Md5 ...
- Android动画学习(一)——Android动画系统框架简介
2015-11-09补充:Drawable Animation极有可能是Frame Animation 这几天在找工作,面试的时候被问到了Android动画,之前完全没接触过这部分,直接给懵了,当然其 ...
- ZOJ Problem Set - 1048 Financial Management
我承认这是一道水的不能再水的题,今天一下就做到了,还是无耻的帖上来吧 #include <stdio.h> int main() { double sum=0; for(int i=1;i ...
- ime-mode:disabled 关闭文本框输入法
在用户输入数字的表单中,需要禁止用户输入中文.符号等,减少用户输入出错误的可能性,CSS可以实现此功能. ime-mode的语法解释如下: ime-mode : auto | active | ina ...
- 用Vagrant创建Jenkins构建环境
这是一个关于Vagrant的学习系列,包含如下文章: Vagrant入门 创建自己的Vagrant box 用Vagrant搭建Jenkins构建环境 用Vagrant和Ansible搭建持续交付平台 ...
- C# 热敏打印机 Socket 网络链接 打印 图片 (一)
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using Syste ...
- C# DataGrid根据某列的内容设置行字体加粗 单元格设置对齐方式
最近做了个功能,DataGrid显示具体内容的时候,根据某列分组. 每个分组具体内容后边,增加一行显示合计信息. 查询数据时,使用了union all将分组数据与明细数据合并起来,使用了排序达到了预期 ...