iOS搜索框的实现

iOS搜索框的实现

引子:

搜索框是我们在实际开发中比较常用到的控件之一,可以说几乎每一个应用程式中都会使用到搜索框,例如QQ、微信等都用到了搜索框

这里写图片描述

这里写图片描述

iOS中的搜索框实现起来相对简单一点,实现方法大致有iOS8.0之前的利用UISearchBar和UIDisplayController实现,这种方法实现效果是没有问题的,但是会报警告,意思是iOS8.0之后不推荐使用这种方法,也就是不推荐使用UISearchDisplayDelegate ,但是可以通过 UISearchController 实现 UISearchResultsUpdating 这个委托同样可以实现上面的效果。在这里呢,我们只讨论后一种方法。

一、实现步骤

1. 遵守代理协议
/*
这里我们主要用到了UISearchResultsUpdating这个协议的-(void)updateSearchResultsForSearchController:(UISearchController *)searchController方法。作用是当搜索框的内容或操作发生变化、操作框成为第一相应者时调用此方法,这是我们实现搜索操作的主要方法。
*/
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchResultsUpdating>
2. 声明属性
//搜索控制器
@property (nonatomic, strong) UISearchController *searchController;
//存放tableView中显示数据的数组
@property (strong,nonatomic) NSMutableArray  *dataList;
//存放搜索列表中显示数据的数组
@property (strong,nonatomic) NSMutableArray  *searchList;
3. 初始化属性
- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化数组并赋值
    self.dataList=[NSMutableArray arrayWithCapacity:100];
    for (NSInteger i=0; i<100; i++) {
        [self.dataList addObject:[NSString
        stringWithFormat:@"%ld-FlyElephant",(long)i]];
    }

    //初始化UISearchController并为其设置属性
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    //设置代理对象
    _searchController.searchResultsUpdater = self;
    //设置搜索时,背景变暗色            _searchController.dimsBackgroundDuringPresentation = NO;
    //设置搜索时,背景变模糊
_searchController.obscuresBackgroundDuringPresentation = NO;
    //隐藏导航栏                  _searchController.hidesNavigationBarDuringPresentation = NO;
    //设置搜索框的frame
    _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    //将搜索框设置为tableView的组头
    self.tableView.tableHeaderView = self.searchController.searchBar;
}
4. 返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //如果searchController被激活就返回搜索数组的行数,否则返回数据数组的行数
    if (self.searchController.active) {
        return [self.searchList count];
    }else{
        return [self.dataList count];
    }
}
5. 返回cell的内容

tableView显示的内容就不再赘述,这里主要讨论与搜索相关的内容

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *flag=@"cellFlag";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
    }
    //如果搜索框被激活,就显示搜索数组的内容,否则显示数据数组的内容
    if (self.searchController.active) {
        [cell.textLabel setText:self.searchList[indexPath.row]];
    }
    else{
        [cell.textLabel setText:self.dataList[indexPath.row]];
    }
    return cell;
}
6. 执行过滤操作
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    //获取搜索框中用户输入的字符串
    NSString *searchString = [self.searchController.searchBar text];
    //指定过滤条件,SELF表示要查询集合中对象,contain[c]表示包含字符串,%@是字符串内容
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    //如果搜索数组中存在对象,即上次搜索的结果,则清除这些对象
    if (self.searchList!= nil) {
        [self.searchList removeAllObjects];
    }
    //通过过滤条件过滤数据
    self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
    //刷新表格
    [self.tableView reloadData];
}

二、实现效果

这里写图片描述

这里写图片描述

这里写图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值