zoukankan      html  css  js  c++  java
  • UISearchController

    • 示例

    //  SearchViewController.m
    
    @interface WYSearchViewController ()<UISearchBarDelegate, UISearchResultsUpdating>
    
    @property(nonatomic, strong) UISearchController *searchVC;
    @property(nonatomic, strong) WYSearchResultTableViewController *resultVC;
    
    #pragma mark - UISearchBarDelegate
    
    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
        return YES;
    }
    
    - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
        
        return YES;
    }
    
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        searchBar.showsCancelButton = YES;
        for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
            if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
                UIButton * cancel =(UIButton *)view;
                if (searchText.length) {
                    [cancel setTitle:@"搜索" forState:UIControlStateNormal];
                } else {
                    [cancel setTitle:@"取消" forState:UIControlStateNormal];
                }
            }
        }
        
    }
    
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
        [self.searchVC.searchBar resignFirstResponder];
    }   // 键盘
    
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        [self.searchVC.searchBar resignFirstResponder];
    }
    
    #pragma mark - search result delegate
    
    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
        if (searchController.searchBar.text.length) {
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        } else {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        }
        
        BaiduLocation *baiduSearch = [BaiduLocation sharedBaiduLocate];
        baiduSearch.poiBlock = ^(NSArray *poiArray){
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            self.historyArray = [poiArray mutableCopy];
            self.resultVC.historyArray = self.historyArray;
            [self.resultVC.tableView reloadData];
        };
        [baiduSearch startPOIsearch:self.searchVC.searchBar.text];
    }
    - (UISearchController *)searchVC {
        if (!_searchVC) {
            _searchVC = [[UISearchController alloc] initWithSearchResultsController:self.resultVC];
            _searchVC.searchResultsUpdater = self;
            _searchVC.dimsBackgroundDuringPresentation = NO;
            _searchVC.searchBar.placeholder = @"大家都在搜:齐来大厦";
            _searchVC.searchBar.delegate = self;
            _searchVC.searchBar.returnKeyType = UIReturnKeySearch;
        }
        return _searchVC;
    }

      

    searchResultViewController

    // SearchResultViewController.m
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self initUI];
    }
    
    - (void)initUI {
        self.tableView.backgroundColor = [UIColor colorWithRed:231.0/255 green:231.0/255 blue:231.0/255 alpha:1];
        self.tableView.tableFooterView = [[UIView alloc] init];
    }
    
    #pragma mark - Table view delegate
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.historyArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
        }
        NSDictionary *poiDic = self.historyArray[indexPath.row];
        cell.textLabel.text = poiDic[@"keyword"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@", poiDic[@"city"], poiDic[@"district"]];
        
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"selectKeyword" object:nil userInfo:self.historyArray[indexPath.row]];
    }
    
    - (NSArray *)historyArray {
        if (_historyArray == nil) {
            _historyArray = [NSArray new];
        }
        return _historyArray;
    }
    

      

  • 相关阅读:
    js显示当前时间
    js平滑循环滚动,广告图片文字…
    记录报错Cannot set property 'innerHTML' of null
    回到顶部的几种方法
    vue表单验证 Vee-Validate 的使用
    Vue @mouseover和@mouseout实现下拉二级导航
    线性代数学习笔记——第四章
    线性代数学习笔记——第三章
    线性代数学习笔记——第二章(下)
    线性代数学习笔记——第二章(上)
  • 原文地址:https://www.cnblogs.com/dzhs/p/5758891.html
Copyright © 2011-2022 走看看