年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3559|回复: 2

UISearchBar与UISearchDisplayController

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2015-6-1 13:59:53 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2015-6-1 14:02 编辑

    1、一开始呢我是搞不清楚这两个东西有什么关系,或者说UISearchDisplayController我根本就没用过,那么就先来看下UISearchBar的使用效果

    2、UISearchBar是个搜索工具条,这个控件的工作原理非常简单,他与UITextField其实有点像,能够监听开始搜索,搜索词变化,搜索结束等一系列状态,并调用代理的相关方法,这些功能UITextField不是也都有吗?只是他是专业的搜索控件,长得好看一点而已!

    3、来,看下UISearchBar在TableView中的表现吧,两张图给你看出效果:

    iOS Simulator Screen Shot 2015年6月1日 下午1.42.55.png    iOS Simulator Screen Shot 2015年6月1日 下午1.42.59.png

    4、如何实现呢?
    [Objective-C] 纯文本查看 复制代码
    //
    //  ViewController.m
    //  Test
    //
    //  Created by 余西安 on 15/6/1.
    //  Copyright (c) 2015年 Sian. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController () <UISearchDisplayDelegate, UISearchBarDelegate>
    
    @property (nonatomic, strong) NSMutableArray    *dataArray;
    
    @property (nonatomic, strong) NSArray           *searchArray;
    
    //@property (nonatomic, strong) UISearchDisplayController *searchDisplay;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.dataArray = [NSMutableArray arrayWithObjects:@"a", @"b", @"c", @"d", @"e", nil];
        self.searchArray = [self.dataArray mutableCopy];
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
        searchBar.delegate = self;
        self.tableView.tableHeaderView = searchBar;
    //    self.searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    //    self.searchDisplay.searchResultsDataSource = self;  // 搜索结果表格DataSource
    //    self.searchDisplay.searchResultsDelegate = self;    // 搜索结果表格Delegate
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.dataArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SATestCell"];
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"SATestCell"];
        }
        cell.textLabel.text = self.dataArray[indexPath.row];
        return cell;
    }
    
    #pragma mark - UISearchBar代理方法
    /// 搜索框内容发生变化时调用
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        [self.dataArray removeAllObjects];
        for (NSString *string in self.searchArray) {
            if ([string rangeOfString:searchText].length || searchText.length == 0){
                [self.dataArray addObject:string];
            }
        }
        [self.tableView reloadData];    // 如果使用UISearchDisplayController这句代码则没必要
    }
    /// 取消搜索时调用
    - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
    {
        self.dataArray = [self.searchArray mutableCopy];
        [self.tableView reloadData];
    }
    
    @end

    5、再来看看UISearchDisplayController是个什么东西,从上图来看,普通的UISearchBar的搜索结果在原有的TableView上展示,当取消时还原原有的所有数据,回到初始状态。

    6、UISearchDisplayController则在此基础上做了一点点加工,主要体现在两个方面:第一、搜索结果不在原有的表格上展示,而是在一个新的TableView上显示搜索结果;第二、将搜索页面全屏显示,让用户专注于搜索过程,更简洁美观。
    注意:UISearchDisplayController不能单独工作,他需要基于某个UISearchBar!!

    7、如果不很清楚,来,看下效果图:

       iOS Simulator Screen Shot 2015年6月1日 下午1.43.12.png    iOS Simulator Screen Shot 2015年6月1日 下午1.43.15.png

    8、代码实现与上面的一致,把注释掉的部分打开即可,事实上就多了那么几行:
    [Objective-C] 纯文本查看 复制代码
    @property (nonatomic, strong) UISearchDisplayController *searchDisplay;
        self.searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
        self.searchDisplay.searchResultsDataSource = self;  // 搜索结果表格DataSource
        self.searchDisplay.searchResultsDelegate = self;    // 搜索结果表格Delegate

    9、Demo下载

    游客,如果您要查看本帖隐藏内容请回复
  • TA的每日心情
    犯困
    2015-1-11 14:36
  • 签到天数: 5 天

    [LV.2]偶尔看看I

    发表于 2015-6-26 15:23:37 | 显示全部楼层
    感谢楼主分享

    该用户从未签到

    发表于 2015-12-18 13:59:51 | 显示全部楼层
    12312345678
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-26 05:33 , Processed in 0.054256 second(s), 21 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表