大醉和尚  
qq:3262641915,如有问题,欢迎讨论.
  • 示例

//  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;
}

  

posted on 2016-08-10 21:11  大醉和尚  阅读(416)  评论(0编辑  收藏  举报