iOS之默认搜索UISearchDisplayController

废话少说直接上代码:


@interface JSLiveViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate, JSProfessionScrollViewDelegate>


/**

 *  搜索框

 */

@property (nonatomic, strong) UISearchBar *mySearchBar;


/**

 *  搜索结果显示

 */

@property (nonatomic, strong) UISearchDisplayController *mySearchDisplayController;



    //3.添加搜索框

    [self setupMysearchBarAndMysearchDisPlay];


/**

 *  设置tablview顶部搜索框

 *  创建UISearchBar

 *  创建UISearchDisplayController

 */

-(void)setupMysearchBarAndMysearchDisPlay

{

    self.mySearchBar = [[UISearchBar alloc]init];

    self.mySearchBar.placeholder = @"搜索老师或者房间号";

    self.mySearchBar.delegate = self;


    [self.mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];

    [self.mySearchBar sizeToFit];

    self.mySearchBar.backgroundColor = JSColor(200, 200, 200);

    self.mySearchBar.backgroundImage = [UIImage imageWithColor:[UIColor clearColor] size:self.mySearchBar.bounds.size];

    //加入列表的header里面

    self.tableView.tableHeaderView = self.mySearchBar;

    

    self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.mySearchBar contentsController:self];

    self.mySearchDisplayController.searchResultsTableView.rowHeight = self.tableView.rowHeight;

    self.mySearchDisplayController.delegate = self;

    self.mySearchDisplayController.searchResultsDataSource = self;

    self.mySearchDisplayController.searchResultsDelegate = self;

}


/**

 *  开始编辑时改变<取消>按钮的文字

 *

 *  @param searchBar

 */

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

{

    self.mySearchBar.showsCancelButton = YES;

    

    NSArray *subViews = [self.mySearchBar.subviews[0] subviews];

    

    for (id view in subViews)

    {

        if ([view isKindOfClass:[UIButton class]])

        {

            UIButton* cancelbutton = (UIButton* )view;

            [cancelbutton setTitle:@"取消" forState:UIControlStateNormal];

            break;

        }

    }


}


/**

 *  准备搜索前,把TableView调成全屏幕

 */

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar

{

    return YES;

}


/**

 *  结束搜索时候恢复原状

 */

-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar

{

    return YES;

}


/**

 * SearchBar输入内容有变化的时候,则执行此方法,询问要不要重装searchResultTableView的数据

 * Return YES to cause the search result table view to be reloaded.

 */

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{

    //根据用户的输入的关键字searchString,处理出搜索结果(可以是本地搜索,可以是从服务器请求过来的数据)到数组

    [self filterContentForSearchText:searchString];

    

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.001);

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        for (UIView* v in self.mySearchDisplayController.searchResultsTableView.subviews)

        {

            if ([v isKindOfClass: [UILabel class]] && [[(UILabel*)v text] isEqualToString:@"No Results"])

            {

                UILabel *lbl = (UILabel*)v;

                lbl.text = @"没有需要显示的结果";

                break;

            }

        }

    });

    

    return YES;

}


/**

 *  如果设置了选项,当Scope Button选项有变化的时候,则执行此方法,询问要不要重装searchResultTableView的数据

 *  Return YES to cause the search result table view to be reloaded.

 */

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption

{

    [self filterContentForSearchText:self.mySearchBar.text];

    

    return YES;

}


/**

 *  是否包含或等于要搜索的字符串内容

 *

 *  @param searchText searchText 要查找的内容

 *  @param scope 范围

 */

-(void)filterContentForSearchText:(NSString*)searchText

{

    NSMutableArray *tempResults = [NSMutableArray array];

    //不区分大小写 | 忽略 "-" 符号的比较

    NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;

    

    for (int i = 0; i < self.liveCellVoMutableArray.count; i++)

    {

        JSLiveCellVo *storeCellVo = [self.liveCellVoMutableArray objectAtIndex:i];

        //教师名称查找

        NSString *storeString = [[self.liveCellVoMutableArray objectAtIndex:i] teacherName];

        NSRange storeRange = NSMakeRange(0, storeString.length);

        NSRange foundRange = [storeString rangeOfString:searchText options:searchOptions range:storeRange];

        //班级名称查找

        NSString *roomStr = [[self.liveCellVoMutableArray objectAtIndex:i] courseRoom];

        NSRange roomRange = NSMakeRange(0, roomStr.length);

        NSRange foundRoomRange = [roomStr rangeOfString:searchText options:searchOptions range:roomRange];

        

        if (foundRange.length || foundRoomRange.length)

        {

            [tempResults addObject:storeCellVo];

        }

    }

    

    [self.searchResultsData removeAllObjects];

    [self.searchResultsData addObjectsFromArray:tempResults];

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值