美文网首页
UISearchBar和UISearchDisplayContr

UISearchBar和UISearchDisplayContr

作者: 花小蓉 | 来源:发表于2016-12-26 16:07 被阅读642次

    UISearchBar和UISearchDisplayController配合使用,是iOS8之前的使用方法,iOS8以后使用UISearchController,更方便简单。

    遵守协议<UISearchBarDelegate, UISearchDisplayDelegate>

    @property(nonatomic,strong) UISearchBar *searchBar;

    @property(nonatomic,strong) UISearchDisplayController *searchDisplayController;

    @property(nonatomic,strong) NSMutableArray *searchResultArray;//搜索结果数组

    初始化控件

    #pragma mark - getter

    - (UISearchBar*)searchBar {

    if(!_searchBar) {

    _searchBar= [[UISearchBar alloc]init];

    _searchBar.delegate=self;

    _searchBar.tintColor= [YFRUtility colorFromHexString:@"0x3f75ff"];//设置bar字体前景色

    _searchBar.barTintColor= [YFRUtility colorFromHexString:@"0xEDEDF3"];//bar前景色

    _searchBar.layer.borderColor= [YFRUtility colorFromHexString:@"0xEDEDF3"].CGColor;

    _searchBar.layer.borderWidth=0.5;//为了掩饰黑线,加边框设置前景色同色

    [_searchBar sizeToFit];

    }

    return _searchBar;

    }

    - (UISearchDisplayController*)searchDisplayController {

    if(!_searchDisplayController) {

    _searchDisplayController= [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];

    _searchDisplayController.delegate=self;

    _searchDisplayController.searchResultsDataSource=self;

    _searchDisplayController.searchResultsDelegate=self;

    _searchDisplayController.searchResultsTableView.tableFooterView= [UIView new];//无数据时不显示cell

    }

    return_searchDisplayController;

    }

    viewDidLoad代码

    - (void)viewDidLoad {

    [super viewDidLoad];

    self.searchResultArray = [NSMutableArray array];

    self.tableView.tableHeaderView = self.searchBar;

    self.edgesForExtendedLayout=UIRectEdgeNone;//解决页面跳转时searchBar跳动问题

    }

    - (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    //跳转页面时searchDisplayController置为不活跃

    if(_searchDisplayController.isActive) {

    _searchDisplayController.active=NO;

    }

    }

    实现UISearchBar的代理回调方法

    #pragma mark - UISearchBar Delegate

    //实时搜索

    - (void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText {

    [self searchDataWithKeyword:searchText];//执行搜索代码

    }

    //点击搜索按钮

    - (void)searchBarSearchButtonClicked:(UISearchBar*)searchBar {

    [self searchDataWithKeyword:searchBar.text];//执行搜索代码

    }

    //取消搜索

    - (void)searchBarCancelButtonClicked:(UISearchBar*)searchBar {

    [self.searchResultArray removeAllObjects];//取消搜索清空result数组

    }

    搜索数据的方法

    - (void)searchDataWithKeyword:(NSString*)keyword {

    [self.searchResultArray removeAllObjects];//清空之前的数据

    //网络请求或本地搜索

    {

    //self.searchResultArray赋值并更新结果列表

    [_searchDisplayController.searchResultsTableView reloadData];

    }

    }

    //解决tableView下拉刷新冲突

    #pragma mark UIScrollViewDelegate

    - (void)scrollViewDidScroll:(UIScrollView*)scrollView {

    if(_searchDisplayController.isActive) {

    return;

    }

    ...//下拉刷新代码

    }

    Tableview的回调:区分原始tableview和resultTableview

    #pragma mark -- UITableViewDelegate

    -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    if(_searchDisplayController.isActive) {//搜索结果

    return [self.resultArray count];

    }else{//原始tableview

    return [self.dataSource count];

    }

    }

    - (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath

    {

    if(_searchDisplayController.isActive) {//搜索结果

    return 44;

    }else{//原始tableview

    return 60;

    }

    }

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

    staticNSString*identifier =@"";

    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if(!cell) {

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    if(_searchDisplayController.isActive) {//搜索结果

    }else{//原始tableview

    }

    returncell;

    }

    -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if(_searchDisplayController.isActive) {//搜索结果

    }else{//原始tableview

    }

    }

    相关文章

      网友评论

          本文标题:UISearchBar和UISearchDisplayContr

          本文链接:https://www.haomeiwen.com/subject/uvkkvttx.html