iOS8之后搜索框的常规实例
1、在一个二级导航控制器中添加一个UITableviewController作为子控制器
2、UITableviewController.tableView 作为展示结果
3、利用iOS之后的UISearchController 根据输入更新输入结果
@interface WJWSearchViewController ()<UISearchResultsUpdating,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableViewController *searchTableViewController;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *searchList;
@end
@implementation WJWSearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self addChildViewController:self.searchTableViewController];
[self initDataArray];
[self configUI];
}
- (void)initDataArray{
[self.dataArray addObject:@"runLoop"];
[self.dataArray addObject:@"gcd"];
[self.dataArray addObject:@"timer"];
[self.dataArray addObject:@"sideBar"];
[self.dataArray addObject:@"searchBar"];
[self.dataArray addObject:@"fmdb"];
[self.dataArray addObject:@"afnetworking"];
}
- (void)configUI {
[self.view addSubview: self.searchTableViewController.tableView];
self.searchTableViewController.tableView.frame = self.view.frame;
}
#pragma mark ---UITableViewDataSource---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.searchController.active) {
return [self.searchList count];
}else {
return self.dataArray.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *searchCellId = @"SearchCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:searchCellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:searchCellId];
}
if (self.searchController.active) {
[cell.textLabel setText:self.searchList[indexPath.row]];
}else {
[cell.textLabel setText:self.dataArray[indexPath.row]];
}
return cell;
}
#pragma mark ----UISearchResultsUpdating----
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchList != nil) {
[self.searchList removeAllObjects];
}
//过滤数据
self.searchList = [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:predicate]];
//刷新表格
[self.searchTableViewController.tableView reloadData];
}
-(UISearchController *)searchController {
if (!_searchController) {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = YES;//搜索框编辑时隐藏导航栏
_searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, _searchController.searchBar.frame.origin.y, _searchController.searchBar.frame.size.width, 44.0);
self.searchTableViewController.tableView.tableHeaderView = _searchController.searchBar;
}
return _searchController;
}
- (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
- (UITableViewController *)searchTableViewController {
if (!_searchTableViewController) {
_searchTableViewController = [[UITableViewController alloc] init];
_searchTableViewController.tableView.delegate = self;
_searchTableViewController.tableView.dataSource = self;
}
return _searchTableViewController;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
遇到的问题:
模拟器无法中文输入,
解决方法:
1、在Xcode中给项目的Scheme设置目标项目的 所属区域。默认一般使系统,可以设置为china。edit scheme-> option ->application region :china
2、在模拟器中的通用->语言地区->iPhone : 简体中文
思考:关于拼音搜索,首字母搜索,等模糊搜索未实现。
iOS8之后搜索框的常规实例的更多相关文章
- iOS搜索框
在iOS8以前搜索框是作为一个控件添加到TableViewController中, 有系统自带的搜索变量self.searchDisplayController 遵守一个搜索显示的协议<UISe ...
- C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例
C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...
- Ajax跨域:Jsonp实例--百度搜索框下拉提示
Ajax跨域:Jsonp实例--百度搜索框下拉提示 一.总结 一句话总结:a.找好接口:b.用script标签的src引入文件(json数据):c.定义及实现上一步引入文件中的函数 1.如何找到一个网 ...
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- Dom实例:数据自增、搜索框及跑马灯
数据自增 功能:当点击add按扭后,div标签中的数据自动+1,实现网页的动态化 <!DOCTYPE html> <html lang="en"> < ...
- Javascript实例 -- 计时器, 搜索框,selected联动
计时器: <body> <input type="text" id="i1"> <input type="button& ...
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- 三、jQuery--jQuery实践--搜索框制作
input标签讲解 <input/>作为按钮的type属性:button.submit(后面会有二者对比分析)
- 搜索框(SearchView)的功能与用法
SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索. 使用SearchView时可以使用如下 ...
随机推荐
- 设计模式七: 策略(Strategy)
简介 策略属于行为型模式的一种,策略模式允许对象的行为或算法在运行时改变,使用不同的算法达成相同的结果或目的. 实现层面上,定义一个抽象的算法接口, 然后根据具体算法的不同定义不同的类去实现该接口, ...
- word2vec概述
既然是概述,那么我也只会在文中谈一点关于 Word2Vec 的思想和大概的方法.对于这个算法,如果一开始学习就深入到算法细节中,反而会陷入局部极值点,最后甚至不知道这个算法是干嘛的.在了解算法大概的思 ...
- 更改Ubuntu默认python版本的方法
当你安装 Debian Linux 时,安装过程有可能同时为你提供多个可用的 Python 版本,因此系统中会存在多个 Python 的可执行二进制文件.一般Ubuntu默认的Python版本都为2. ...
- gcc 8.2.1 / MCF thread 简介
gcc 8.2.1 下载 地址 https://gcc-mcf.lhmouse.com/ MCF threadhttps://github.lhmouse.com/ MCF thread 简介MCF ...
- 【原创】大叔问题定位分享(28)openssh升级到7.4之后ssh跳转异常
服务器集群之间忽然ssh跳转不通 # ssh 192.168.0.1The authenticity of host '192.168.0.1 (192.168.0.1)' can't be esta ...
- 微信小程序picker组件 - 省市二级联动
picker 从底部弹起的滚动选择器,现支持五种选择器,通过mode来区分,分别是普通选择器,多列选择器,时间选择器,日期选择器,省市区选择器,默认是普通选择器. picker官方文档链接 由于项目需 ...
- Ubuntu18.04安装Python虚拟环境
仅为使用Ubuntu18.04的Python开发人员作参考 1.安装Ubuntu18.04虚拟环境 sudo apt install virtualenv sudo apt install virtu ...
- Web微信模拟
一.概要 目的:实现一个具有web微信类似功能的项目 框架:Django 模块:render.HttpResponse.BeautifulSoup.re.time.requests.json.rand ...
- SonarLint 代码质量管理
Below are the instructions of how to install and use SonarLint. Install SonarLint Extensions in VS20 ...
- mysql根据分组和条件查询以后如何统计记录的条数
1.子查询,查询出的数据随便起一个别名,然后根据分组和条件查询出的数据,作为一个具有一列的一个表,然后外面的查询查询这个数据表的这一列的总数,即可. SELECT COUNT( * ) FROM ( ...