使用搜索条UISearchBar 谓词实现搜索的判断

来源:互联网 发布:mac怎样卸载程序 编辑:程序博客网 时间:2024/05/21 17:59

searchBar有可以设置搜索文本框上方的标题的属性,右侧取消按钮(还有书签按钮,查询结果按钮)也都可以设置是否有,搜索框的颜色,还可设置搜索条的风格。

看下面第二副图,我们可以设置带分段的搜索条,该属性Shows scope Bar 与 Scope Titles。当用户单击分段条上指定的分段按钮时,系统将会激发一个方法,从而允许程序通过该方法控制只对指定范围的数据执行搜索。点击每一部分都有自己的代理方法实现


下方是实现搜索的具体代码:

首先我们要做的是在stroy中建立tableView和searchBar 并且要和IBOutlet关联到代码当中:然后具体看下方

////  ViewController.m//  11111////  Created by xxt-imac on 16/1/14.//  Copyright © 2016年 xxt-imac. All rights reserved.//#import "ViewController.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>@property (strong, nonatomic) IBOutlet UITableView *table;@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;//保存原始表格数据的数组@property(strong,nonatomic)NSArray *dataArray;//保存搜索结果的数据的数组@property(strong,nonatomic)NSArray *searchArray;@property(assign,nonatomic)BOOL isSearch;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //初始化表格数据    _dataArray = [NSArray arrayWithObjects:@"哦;四姐夫",@"洛杉矶阿道夫徕卡是否",@"了快速和覅可还是",@"螺丝哦哈覅覅",@"阿斯顿客服或类似回复",@"阿里山螺丝回复",@"离开iu吧后is啊",@"苦海可能了卡舒服2",@"上来看回复了可能",@"客户是否老",@"刻录哈舒服老",@"ASF老卡是否",@"哦i换肤离开",@";拉丝粉开老老地方",@"老师发来看你撒", nil];    self.table.delegate =self;    self.table.dataSource = self;        _isSearch = NO;    //设置搜索条的delegate    self.searchBar.delegate =self;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    //如果处于搜索状态    if (_isSearch) {        //使用searchArray作为表格显示的数据        return _searchArray.count;    }else    {        //否则使用原始的dataArray作为表格的显示的数据        return _dataArray.count;    }}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellID  = @"cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];    if (cell == nil) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];    }    NSInteger rowNo = indexPath.row;    //如果处于搜索状态    if (_isSearch) {        //使用searchArray作为表格显示的数据        cell.textLabel.text = [_searchArray objectAtIndex:rowNo];    }else{        cell.textLabel.text = [_dataArray objectAtIndex:rowNo];    }    return cell;}-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{    //取消搜索状态    _isSearch = NO;    [self.searchBar resignFirstResponder];    [self.table reloadData];}//当搜索框中的文本发生变化时-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{    [self filterBySubstring:searchText];}//当用户点击虚拟键盘上的search按钮时激发该方法-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{    [self filterBySubstring:searchBar.text];    [self.searchBar resignFirstResponder];}-(void)filterBySubstring:(NSString *)subStr{    //设置搜索状态    _isSearch = YES;    //定义谓词    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@",subStr];    //使用谓词过滤NSArray    _searchArray = [_dataArray filteredArrayUsingPredicate:pred];    //让表格控件重新加载数据    [self.table reloadData];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

这是搜索前和搜索后的对比图片,可以参考

  搜索前的图片:                              搜索后的图片:



补充:本文还有一个重点要掌握的就是谓词

//定义谓词    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@",subStr];    //使用谓词过滤NSArray    _searchArray = [_dataArray filteredArrayUsingPredicate:pred];    //让表格控件重新加载数据
首先定义谓词,然后执行
上方代码就是实现了dataArray中的string不管有什么substr与searchArray中的string中的substr一样,那么dataArray中的string就会被挑选出来放在searchArray当中。简单的说就是不管dataArray中那个字与searchArray中的任何一个字相等,那么有这个字的字符串就会成为searchArray中的一员。

4 0
原创粉丝点击