美团HD(8)-利用NSPredicate匹配搜索结果

监听文本框改变:

DJSelectCityViewController.m

复制代码
/** 当searchBar内的文字发生改变时调用此方法 */
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    UIView *cover = [self.view viewWithTag:DJCoverTag];
    if (searchText.length) { // 当前输入内容不为空
        if(cover.subviews.count <= 0) {
            cover.alpha = 1.0;
            self.searchResultVC.view.frame = CGRectMake(0, 0, cover.width, cover.height);
            [cover addSubview:self.searchResultVC.view];
        }
        // 将当前内容传递给 DJSearchCityResultViewController 以进行搜索
        [self.searchResultVC setSearchText:searchText];
    } else { // 当前输入内容为空
        [self.searchResultVC.view removeFromSuperview];
         cover.alpha = 0.2;
    }
    
}
复制代码

DJSearchCityResultViewController.m

复制代码
#import "DJSearchCityResultViewController.h"
#import "MJExtension.h"
#import "DJCity.h"

@interface DJSearchCityResultViewController ()

/** 城市列表 */
@property (nonatomic,strong) NSArray *citiesList;
/** 搜索匹配到的结果 */
@property (nonatomic,strong) NSArray *matchSearchResults;

@end


@implementation DJSearchCityResultViewController


/** 加载城市列表 */
- (NSArray *)citiesList {

    if (!_citiesList) {
        _citiesList = [DJCity mj_objectArrayWithFilename:@"cities.plist"];
    }
    return _citiesList;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}


/** 设置搜索内容 */
- (void)setSearchText:(NSString *)searchText {

    // 将待搜索字符串转换成小写
    NSString *searchFormat = searchText.lowercaseString;
    // 使用谓词进行搜索
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains %@ or pinYin contains %@ or pinYinHead contains %@",searchFormat,searchFormat,searchFormat];
    self.matchSearchResults = [self.citiesList filteredArrayUsingPredicate:predicate];
    // 匹配完成后刷新tableView
    [self.tableView reloadData];

}

#pragma mark - UITableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.matchSearchResults.count;

}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return [NSString stringWithFormat:@"搜索到%ld条结果",self.matchSearchResults.count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *ID = @"matchResult";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    DJCity *city = self.matchSearchResults[indexPath.row];
    cell.textLabel.text = city.name;
    return cell;
}


@end
复制代码

最终结果:

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(383)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示