iOS学习笔记-039.UISearBar——基本使用

UISearBar——基本使用

一、UISearchBar——方便用户搜索信息

在移动应用程序的世界里,用户对信息获取的速度要求非常高!iOS用户希望他们需要的信息能够迅速地,直观地展现在他们面前
因为UITableView的上下滚动能让用户迅速,自然地浏览大量信息,许多基于UIKit的应用都使用了UITableView来组织信息。但如果信息量非常非常大,让用户上下滚动如此长的列表是非常没有效率的。所以一个搜索的功能就是必须的了
UISearchBar控件,能让用户迅速的筛选有用的信息!
今时今日,用户在应用里看到很长的列表时,都会期待一个搜索的功能。如果他们找不到搜索功能,他们会非常的沮丧

二、UISearchBar工作原理

其本身不做任何搜索,而仅是提供一个基本的iOS搜索栏界面
UISearchBar类用delegate协议的方式来告诉应用程序的其他部分,用户正在搜索栏中做什么
程序员需要自己编写对比字符串和过滤搜索的方法

注意:实际应用中需要使用Search Bar and Search Display Controller

三、为什么是Search Bar and Search Display Controller ?

搜索显示控制器用来控制一个搜索栏以及一个table view。这个table view会显示搜索过滤后的信息,而这些信息来源于另一个视图控制器
即“search display controller”(搜索显示控制器)需要知道table view controller控制的数据,然后在自己的table view中显示搜索过滤后的结果。这个table view会覆盖table view controller的视图,这样用户只能看到过滤后的结果

四、UISearchBar的属性

Text: 设置出现在搜索栏中的默认字符串,如果需要指定默认搜索内容时使用

Placeholder: 在搜索栏没有添入任何字符串时,一般会显示一串灰色的字符来提示用户输入搜索信息

Prompt: 属性值会出现在搜索栏上方。对于有复杂搜索功能的程序,用户可能需要一些指导信息

Style & Tint: 设置搜索栏的样式和颜色。建议使用和UINavigationBar上同样的设置

Show Search Results Button: 如果选中此选项,搜索栏右边会出现一个灰色按钮。可以用来显示最近几次的搜索,或者上次搜索的结果,通过Search Bar的代理方法可以控制该按钮的行为

Show Bookmarks Button: 如果选中此选项,搜索栏右边会出现一个标准的蓝色书签按钮。用户可以调出他们储存的书签,通过Search Bar的代理方法可以控制该按钮的行为

Show Cancel Button: 如果选中此选项,搜索栏右边会出现一个标准的取消按钮,让用户取消搜索。初始不要选中此选项,因为当用户在搜索栏中输入内容后,该按钮会自动出现

Shows Scope Bar(显示范围栏) & Scope Titles(范围标题): 帮助用户在指定范围内进行搜索,以缩小搜索范围

Capitalize(大小写)、Correction(自动纠错)、Keyboard(键盘): 与UITextField的属性相同,用于辅助用户输入

五、代码

注意 选择控件是:* Search Bar and Search Display Controller *

//
//  ViewController.m
//  03_UIView32_UISeacherBar
//
//  Created by 杞文明 on 16/1/14.
//  Copyright © 2016年 杞文明. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    NSMutableArray * _dataList;
    NSMutableArray * _searchList;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataList = [[NSMutableArray alloc]init];
    for (NSInteger i=0; i<999; i++) {
        NSString * str = [NSString stringWithFormat:@"小明是-%03ld",i];
        [_dataList addObject:str];
    }
}

#pragma 行数
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(tableView==self.searchDisplayController.searchResultsTableView){
        return _searchList.count;
    }else{
      return _dataList.count;
    }
}

#pragma 单元格
-(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    static NSString * identifier = @"UITableViewCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell==nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    if (tableView==self.searchDisplayController.searchResultsTableView) {
        [cell.textLabel setText:_searchList[indexPath.row]];
    }else{
        [cell.textLabel setText:_dataList[indexPath.row]];
    }

    return cell;
}

#pragma 搜索时过滤数据
-(BOOL)searchDisplayController:(nonnull UISearchDisplayController *)controller shouldReloadTableForSearchString:(nullable NSString *)searchString{
    //1.创建谓词对象
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",searchString];
    //2.清空搜索集合
    if(_searchList!=nil){
        [_searchList removeAllObjects];
    }
    //3.数据过滤
    _searchList = [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:predicate]];
    //4.刷新表格
    return  true;
}
@end

六、图示

这里写图片描述

七、参考blog http://www.tuicool.com/articles/6viqEn

http://www.tuicool.com/articles/6viqEn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值