iOS UISearchController的使用(iOS 8.0以上)

iOS 8.0以后推出的新类UISearchController用来替代UISearchBar+UISearchDisplayController,如果想偷懒可以尝试下。

项目地址:
https://github.com/MisterZhouZhou/UISearchControllerAndUILocalizedCollection

现根据UISearchController的初始化

[UISearchController alloc] initWithSearchResultsController:xx]

进行两种形式的体验。
1、initWithSearchResultsController:nil

1.1 初始化UISearchController:

_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    _searchController.searchResultsUpdater = self;
    _searchController.searchBar.placeholder = @"";
    //设置cancel 为取消
    [_searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
    //是否设置半透明覆盖层(在initWithSearchResultsController:nil的情况下最好设置为NO)
    _searchController.dimsBackgroundDuringPresentation = NO;
    _searchController.searchBar.delegate = self;
    [_searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = _searchController.searchBar;

1.2 设置数据源与显示:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.searchController.active){
      return self.filteredArray.count;
    }
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    if (self.searchController.active){
        cell.textLabel.text = self.filteredArray[indexPath.row];
    }
    else{
        cell.textLabel.text = self.dataArray[indexPath.row];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.searchController.active){
        NSLog(@"%@",self.filteredArray[indexPath.row]);
    }
    else{
         NSLog(@"%@",self.dataArray[indexPath.row]);
    }
}

1.3 数据筛选:

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString * searchString = searchController.searchBar.text;
    NSPredicate * predicate = [NSPredicate  predicateWithFormat:@"SELF CONTAINS %@",searchString];
    self.filteredArray = [[self.dataArray filteredArrayUsingPredicate:predicate] mutableCopy];
    [self.tableView reloadData];
}

2、initWithSearchResultsController:vc

2.1 初始化:

- (void)configureSearchController {
    _resultVC = [SearchResultViewController new];
    _searchController = [[UISearchController alloc] initWithSearchResultsController:_resultVC];
    _searchController.searchResultsUpdater = self;
    _searchController.searchBar.placeholder = @"";
    //设置cancel 为取消
    [_searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
    //这两句设置了设置了似乎也没有什么用,筛选结果页会先下偏移44,
    _searchController.definesPresentationContext = YES;
    _searchController.hidesNavigationBarDuringPresentation = YES;
    //above
    //是否设置半透明覆盖层
    _searchController.dimsBackgroundDuringPresentation = YES;
    _searchController.searchBar.delegate = self;
    [_searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = _searchController.searchBar;
}

2.2 设置筛选数据的显示

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString * searchString = searchController.searchBar.text;
    NSPredicate * predicate = [NSPredicate  predicateWithFormat:@"SELF CONTAINS %@",searchString];
    self.filteredArray = [[self.dataArray filteredArrayUsingPredicate:predicate] mutableCopy];
    _resultVC.dataArray = self.filteredArray;
    [_resultVC.tableView reloadData];
}

效果图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值