码迷,mamicode.com
首页 > 其他好文 > 详细

UISearchController

时间:2016-08-10 22:41:27      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

  • 示例

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

  

UISearchController

标签:

原文地址:http://www.cnblogs.com/dzhs/p/5758891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!