iOS开发 ----- UISearchBar_UISearchController

UISearchBar

简介

提供简单的搜索框,方便用户使用
一般来说配合UISearchController使用
由于iOS8之后不支持UISearchDisplayController,所以建议用上面的

相关属性

位置
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
样式
_searchBar.barStyle = UISearchBarIconSearch;
是否显示取消按钮
_searchBar.showsCancelButton = YES;
是否显示搜索结果按钮
_searchBar.showsSearchResultsButton = YES;
是否显示书签按钮
_searchBar.showsBookmarkButton = YES;
默认提示文字
_searchBar.placeholder = @"请输入文字";
光标的颜色
_searchBar.tintColor = [UIColor greenColor];
取消按钮后者其他按钮的颜色
_searchBar.barTintColor = [UIColor blueColor];

UISearchController

简介

自带一个UISearchBar,提供了一些列的方法,含有两个代理
想要实现搜索功能,必须遵守这个协议UISearchResultsUpdating
初始化,最后的那个self,nil都可以,放到tableView上的时候应该为no
_searchController = [[UISearchController alloc]initWithSearchResultsController:self];
设置代理
_searchController.searchResultsUpdater = self;
设置是否在搜索的时候,主界面半透明,要想搜索出来的数据可以点击的话,要为no
_searchController.dimsBackgroundDuringPresentation = YES;
设置是否隐藏导航栏,一般来说,搜索栏都会放到导航栏上,所以一般都是NO
_searchController.hidesNavigationBarDuringPresentation = NO;
大小位置
_searchController.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
放在导航栏的titleView上
self.navigationItem.titleView = _searchController.searchBar;

代理

必须实现这个方法
执行搜索会调用这个方法,在这个方法中,取到搜索框的字符串,然后在属于组中查找,被查找的数组里边又时一个数组,把相匹配的东西取出来,然后重载tableView
UISearchController有一个active属性,当点击搜索框的时候,active会变成YES,所以在tableView的一些列代理中要改变section和row
点击搜索框的时候调用的方法

-(void)updateSearchResultsForSearchController:(nonnull UISearchController *)searchController
{
    if (_searchArr!=nil) {
        [_searchArr removeAllObjects];
    }

    NSString * str = _searchController.searchBar.text;

    for (NSArray * arr in _dataArr) {
        //_searchArr = [NSMutableArray arrayWithArray:[arr filteredArrayUsingPredicate:prcdicate]];

        for (NSString * string in arr) {
            if ([string rangeOfString:str].location!=NSNotFound) {
                [_searchArr addObject:string];
            }
        }
    }

    [_tableView reloadData];

}
//正则表达式搜索

    NSString  * str = searchController.searchBar.text;

    //SELF MATCHS %@        邮箱的正则
    //SELf CONTAINS [c]%@   是否包含
    NSPredicate * pr = [NSPredicate predicateWithFormat:@"SELf CONTAINS [c]%@",str];

    _searchArray = [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:pr]];
tableView有多少个section,当点击搜索框的时候,使section变为1个
-(NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView
{
    if (_searchController.active) {
        return 1;
    }else
    {
        return _dataArr.count;
    }

}
每个section有多少个row,由于搜索后之后一个section,所以,应该返回的是包含匹配到搜索框内容的数据的那个数组的个数,也就是代理方法中筛选出的那个数组
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (_searchController.active) {
        return _searchArr.count;
    }else
    {
        return [[_dataArr objectAtIndex:section] count];
    }

}
在给cell填数据的时候也是这样,没激活时,是一个数组,激活时是一个数组
-(UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{

    NSString * cellID = @"cellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID ];
    }

    if (_searchController.active) {
        cell.textLabel.text = [_searchArr objectAtIndex:indexPath.row];
    }else
    {

        cell.textLabel.text = [[_dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    }
    return cell;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值