UISearchBar, NSPredicate 实现简单的搜索功能

本文详细介绍了如何在Objective-C中通过数组和UISearchBar代理实现用户输入与数据筛选的互动,包括代理方法的调用、谓词的创建与应用,以及如何在UITableViewController中展示筛选后的数据。

.m文件延展声明一个数组变量用于添加搜索的数据签订UISearchBar的代理

/* 签代理 
 * 创建的VC要继承UITableViewController方便使用
 */
@interface TableViewController () <UISearchBarDelegate>
@property (nonatomic, retain) NSMutableArray *arr;
@end

主函数中添加一个UISearchBar对象简单设置

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = @[@"class 1", @"class 2", @"class 3", @"class 4", @"class 4", @"lanou"].mutableCopy;

    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 100, 375, 80)];
    /* 设置代理 */
    search.delegate = self;
    [self.view addSubview:search];
    [search release];
}

调用代理的输入框监视方法

/* 调用代理方法搜索框输入监测方法 */
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    /* 判断对象是否包含输入的元素信息 */
    /**
     * 谓词用于if语句的条件和数据筛选
     * 通常使用predicateWithFormat:来创建NSPredicate谓词对象
     */
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"self contains [cd] %@", searchText];/**< self 表示要判断包含的对象即下文中的字符串"1234567"  [cd]中c表示不区分大小写 d表示不区分低重音 */
    /* 判断字符串是否包含用户输入的文字 */
    BOOL result = [pred evaluateWithObject:@"1234567"];/* 是否包含用户输入的信息 返回值BOOL类型 */

    /* 对数组中符合要求的数据筛选 */
    /* 方法一: 遍历数组 */
    /* 新定义个数组装符合要求的数据 */
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < self.arr.count; i++) {
        NSString *text = [self.arr objectAtIndex:i];
        /* 符合要求的添加到新定义的数组中 */
        if ([pred evaluateWithObject:text]) {
            [arr addObject:text];
        }
    }
    /* 方法二: 使用数组自带的方法根据创建好的谓词对象筛选(推荐使用) 
     NSMutableArray *arr = (NSMutableArray *)[self.arr filteredArrayUsingPredicate:pred];
    */
    /* 打印结果观察 */
    NSLog(@"%@", arr);
}

注1 : 谓词的常用语法判定条件

BEGINSWITH(以...开始)
The left-hand expression begins with the right-hand expression.
/* 注意:简单举例 
 * 字符串1234567 包含 1, 2, 123 但不包含13, 14, 16
 */
本文中使用的是CONTAINS(包含)
The left-hand expression contains the right-hand expression.
ENDSWITH(以...结束)
The left-hand expression ends with the right-hand expression.
LIKE(是...)
The left hand expression equals the right-hand expression: ? and * are allowed as wildcard characters, where ? matches 1 character and * matches 0 or more characters.
谓词还可以进行大量的判断包括(=, >=, <=, all等判断),详见官方API中Predicate Format String Syntax(谓词语法介绍)

注2 : UITableViewController作为根视图省去写代理和实现代理方法的过程

/* 系统提供设置section 和cell的方法, 只需要简单的改一下 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [self.arr objectAtIndex:indexPath.row];
    return cell;
}

内存管理

- (void)dealloc {
    [_arr release];
    [super dealloc];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值