使用UISearchDisplayController

本文涉及的产品
可视分析地图(DataV-Atlas),3 个项目,100M 存储空间
数据可视化DataV,5个大屏 1个月
简介:

使用UISearchDisplayController

虽然UISearchDisplayController名字中带有controller,可他不是一个UIView相关的controller,因为,切换显示UISearchDisplayController的时候,它并没有在UIWindow中加载(UIWindow一次只能加载一个controller,所以可以证明它不属于controller).

可以看看,他是继承自NSObject,是多个控件组合在一起的一个东西.

以下是使用时的效果图:

源码:

//
//  RootViewController.m
//  TableViewSearch
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()
<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>

@property (nonatomic, strong) UITableView               *dataView;          // tableView
@property (nonatomic, strong) UISearchDisplayController *searchController;  // 搜索控制器

@property (nonatomic, strong) NSArray                   *names;             // 数据源
@property (nonatomic, strong) NSArray                   *searchResults;     // 搜索结果

@end

#define APP_HEIGHT  [UIScreen mainScreen].applicationFrame.size.height
#define SCR_HEIGHT  [UIScreen mainScreen].bounds.size.height
#define SCR_WIDTH   [UIScreen mainScreen].bounds.size.width

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化tableView
    _dataView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, SCR_WIDTH, SCR_HEIGHT - 20)
                                             style:UITableViewStylePlain];
    _dataView.delegate   = self;
    _dataView.dataSource = self;
    [self.view addSubview:_dataView];
    
    // 初始化searchBar
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    searchBar.delegate     = self;
    searchBar.placeholder  = @"搜索";
    [searchBar sizeToFit];
    
    // 将searchBar放置在tableView的HeaderView中
    self.dataView.tableHeaderView = searchBar;

    // 初始化search控制器并获取searchBar以及当前视图控制器
    _searchController = \
        [[UISearchDisplayController alloc] initWithSearchBar:searchBar
                                          contentsController:self];
    _searchController.delegate                = self;
    _searchController.searchResultsDataSource = self;
    _searchController.searchResultsDelegate   = self;
    
    // tableView的数据源
    _names = @[@"Aaliyah", @"Aaron", @"Abigail", @"Adam", @"Addison", @"Adrian", @"Aiden", @"Alex", @"Alexa", @"Alexander", @"Alexandra", @"Alexis", @"Allison", @"Alyssa", @"Amelia", @"Andrea", @"Andrew", @"Angel", @"Anna", @"Annabelle", @"Anthony", @"Aria", @"Ariana", @"Arianna", @"Ashley", @"Aubree", @"Aubrey", @"Audrey", @"Austin", @"Autumn", @"Ava", @"Avery", @"Ayden", @"Bailey", @"Bella", @"Benjamin", @"Bentley", @"Blake", @"Brandon", @"Brayden", @"Brianna", @"Brody", @"Brooklyn", @"Bryson", @"Caleb", @"Cameron", @"Camila", @"Carlos", @"Caroline", @"Carson", @"Carter", @"Charles", @"Charlotte", @"Chase", @"Chloe", @"Christian", @"Christopher", @"Claire", @"Colton", @"Connor", @"Cooper", @"Damian", @"Daniel", @"David", @"Dominic", @"Dylan", @"Easton", @"Eli", @"Elijah", @"Elizabeth", @"Ella", @"Ellie", @"Emily", @"Emma", @"Ethan", @"Eva", @"Evan", @"Evelyn", @"Faith", @"Gabriel", @"Gabriella", @"Gavin", @"Genesis", @"Gianna", @"Grace", @"Grayson", @"Hailey", @"Hannah", @"Harper", @"Henry", @"Hudson", @"Hunter", @"Ian", @"Isaac", @"Isabella", @"Isaiah", @"Jace", @"Jack", @"Jackson", @"Jacob", @"James", @"Jasmine", @"Jason", @"Jaxon", @"Jayden", @"Jeremiah", @"Jocelyn", @"John", @"Jonathan", @"Jordan", @"Jose", @"Joseph", @"Joshua", @"Josiah", @"Juan", @"Julia", @"Julian", @"Justin", @"Katherine", @"Kayden", @"Kayla", @"Kaylee", @"Kennedy", @"Kevin", @"Khloe", @"Kimberly", @"Kylie", @"Landon", @"Lauren", @"Layla", @"Leah", @"Levi", @"Liam", @"Lillian", @"Lily", @"Logan", @"London", @"Lucas", @"Lucy", @"Luis", @"Luke", @"Lydia", @"Mackenzie", @"Madeline", @"Madelyn", @"Madison", @"Makayla", @"Mason", @"Matthew", @"Maya", @"Melanie", @"Mia", @"Michael", @"Molly", @"Morgan", @"Naomi", @"Natalie", @"Nathan", @"Nathaniel", @"Nevaeh", @"Nicholas", @"Noah", @"Nolan", @"Oliver", @"Olivia", @"Owen", @"Parker", @"Peyton", @"Piper", @"Reagan", @"Riley", @"Robert", @"Ryan", @"Ryder", @"Samantha", @"Samuel", @"Sarah", @"Savannah", @"Scarlett", @"Sebastian", @"Serenity", @"Skylar", @"Sofia", @"Sophia", @"Sophie", @"Stella", @"Sydney", @"Taylor", @"Thomas", @"Trinity", @"Tristan", @"Tyler", @"Victoria", @"Violet", @"William", @"Wyatt", @"Xavier", @"Zachary", @"Zoe", @"Zoey"];
}

//===============================================
#pragma mark -
#pragma mark - tableView相关代理
//===============================================
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.dataView)
    {
        return [self.names count];
    }
    else
    {
        return [self.searchResults count];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath
                             animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reusedStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedStr];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:reusedStr];
    }
    
    if (tableView == self.dataView)
    {
        cell.textLabel.text = self.names[indexPath.row];
    }
    else
    {
        cell.textLabel.text = self.searchResults[indexPath.row];
    }
    
    return cell;
}

//===============================================
#pragma mark -
#pragma mark UISearchDisplayController相关代理
//===============================================

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    NSLog(@"将要开始搜索");
}

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
    NSLog(@"开始搜索");
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    NSLog(@"将要结束搜索");
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    NSLog(@"结束搜索");
}

- (void)searchDisplayController:(UISearchDisplayController *)controller
  didLoadSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"加载了tableView");
}

- (void)searchDisplayController:(UISearchDisplayController *)controller
willUnloadSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"卸载了tableView");
}

- (void)searchDisplayController:(UISearchDisplayController *)controller
 willShowSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"将要显示tableView");
}

- (void)searchDisplayController:(UISearchDisplayController *)controller
  didShowSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"已经显示了tableView");
}

- (void)searchDisplayController:(UISearchDisplayController *)controller
 willHideSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"将要隐藏tableView");
}

- (void)searchDisplayController:(UISearchDisplayController *)controller
  didHideSearchResultsTableView:(UITableView *)tableView
{
    NSLog(@"已经隐藏了tableView");
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    NSLog(@"要重新加载tableView显示搜索的数据么?");
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchString];
    self.searchResults = [self.names filteredArrayUsingPredicate:predicate];
    
    return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    NSLog(@"要重新加载tableView的搜索域么?");
    return YES;
}

@end

UISearchDisplayController自带了tableView,也是走的代理方法,与你的controller中的tableView一样的代理方法,这点需要注意:

相关实践学习
DataV Board用户界面概览
本实验带领用户熟悉DataV Board这款可视化产品的用户界面
阿里云实时数仓实战 - 项目介绍及架构设计
课程简介 1)学习搭建一个数据仓库的过程,理解数据在整个数仓架构的从采集、存储、计算、输出、展示的整个业务流程。 2)整个数仓体系完全搭建在阿里云架构上,理解并学会运用各个服务组件,了解各个组件之间如何配合联动。 3&nbsp;)前置知识要求 &nbsp; 课程大纲 第一章&nbsp;了解数据仓库概念 初步了解数据仓库是干什么的 第二章&nbsp;按照企业开发的标准去搭建一个数据仓库 数据仓库的需求是什么 架构 怎么选型怎么购买服务器 第三章&nbsp;数据生成模块 用户形成数据的一个准备 按照企业的标准,准备了十一张用户行为表 方便使用 第四章&nbsp;采集模块的搭建 购买阿里云服务器 安装 JDK 安装 Flume 第五章&nbsp;用户行为数据仓库 严格按照企业的标准开发 第六章&nbsp;搭建业务数仓理论基础和对表的分类同步 第七章&nbsp;业务数仓的搭建&nbsp; 业务行为数仓效果图&nbsp;&nbsp;
目录
相关文章
|
网络安全
rsync报错: Operation not permitted (1)
[rsync报错:rsync: chgrp “.initial-setup-ks.cfg.jaXlVz” (in backup) failed: Operation not permitted (1)] 问题背景:在配置好rsync服务和客户端后,客户端从服务端拉取是正常的,但从客户端推送到服务端报错。 a,单独推送目录会报这个错误 rsync: recv_generator: mkdir “opt” (in backup) failed: Permission denied (13)
1757 0
|
5月前
|
搜索推荐 关系型数据库 MySQL
手把手教你搭建子比主题的系统(亲测可用)|学习版本|虚拟知识付费平台比较合适
搭建Zibll子比主题涉及以下步骤: 1. 服务器环境需支持PHP 7.0+(推荐7.4+)和MySQL。 2. 下载并安装WordPress,可从官方站点获取最新版本。 3. 从指定链接下载子比主题文件。 4. 在WordPress后台上传并启用子比主题,配置固定链接和伪静态,例如使用宝塔面板。 5. 调整主题设置,如Logo和网站关键词。 6. 安装必要插件,如Yoast SEO和Contact Form 7,根据实际需求选择。 7. 完成后测试和调试网站功能。 记得参考官方文档以获取详细指导。
|
JSON HandyJSON Swift
RxSwift+MVVM项目实战-多分组TableView+MJRefresh+RxAlamofire+HandyJSON的使用
RxSwift+MVVM项目实战-多分组TableView+MJRefresh+RxAlamofire+HandyJSON的使用
315 0
|
存储 Swift
RxSwift+MVVM项目实战-多分组UITableView结合RxDataSources的使用
RxSwift+MVVM项目实战-多分组UITableView结合RxDataSources的使用
319 0
RxSwift+MVVM项目实战-多分组UITableView+RxDataSources+MJRefresh的使用
RxSwift+MVVM项目实战-多分组UITableView+RxDataSources+MJRefresh的使用
138 0
|
机器学习/深度学习 算法 Python
吴恩达机器学习--线性回归
吴恩达机器学习--线性回归
125 1
UITableView顶部突然出现一块空白问题
UITableView顶部突然出现一块空白问题
|
负载均衡 前端开发 Java
【Spring cloud】OpenFeign详解(超详细)
【Spring cloud】OpenFeign详解(超详细)
1203 0
|
存储 数据库
数据复制系统设计(2)-同步复制与异步复制
复制的重要可选项: 同步复制,synchronously 异步复制,asynchronously
183 0
|
NoSQL Java 测试技术
基于redis消费队列的一个使用(二)
基于redis消费队列的一个使用(二)
251 0