搜索控制器实现:
typedef void(^SearchTipCallback)(AMapTip *aMapTip);
@interface SearchTipsViewController : UIViewController
@property(nonatomic,copy)SearchTipCallback searchTipCallBack;
@end
#import "SearchTipsViewController.h"
@interface SearchTipsViewController ()<AMapSearchDelegate, UISearchBarDelegate, UISearchResultsUpdating, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) AMapSearchAPI *search;
///搜索框
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *tips;
@end
@implementation SearchTipsViewController
- (id)init
{
if (self = [super init])
{
self.tips = [NSMutableArray array];
}
return self;
}
- (void)back {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
self.searchController.active = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = kBackGroudColor;
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationController.navigationBarHidden = NO;
//导航栏-搜索框
[self navigationItemInit];
//搜索
self.search = [[AMapSearchAPI alloc] init];
self.search.delegate = self;
[self initSearchController];
[self initTableView];
}
- (void)navigationItemInit {
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 44)];
[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:@"back-nav"] forState:UIControlStateNormal];
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -5;
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, item, nil];
}
- (void)initSearchController{
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.placeholder = @"请输入关键字";
[self.searchController.searchBar sizeToFit];
self.navigationItem.titleView = self.searchController.searchBar;
}
- (void)initTableView
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.hidden = YES;
[self.view addSubview:self.tableView];
}
#pragma mark - 搜索
/**
搜索
*/
-(void)searchTipsWithKey:(NSString *)key{
if (key.length == 0) {
return;
}
AMapInputTipsSearchRequest *tips = [[AMapInputTipsSearchRequest alloc] init];
tips.keywords = key;
[self.search AMapInputTipsSearch:tips];
}
#pragma mark - AMapSearchDelegate
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{
// NSLog(@"Error: %@ - %@", error, [ErrorInfoUtility errorDescriptionWithCode:error.code]);
}
/* 搜索的回调 */
- (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response
{
if (response.count == 0)
{
return;
}
[self.tips setArray:response.tips];
[self.tableView reloadData];
}
#pragma mark - UISearchResultsUpdating
//搜索器更新
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
self.tableView.hidden = !searchController.isActive;
[self searchTipsWithKey:searchController.searchBar.text];
if (searchController.isActive && searchController.searchBar.text.length > 0)
{
searchController.searchBar.placeholder = searchController.searchBar.text;
}
}
#pragma mark - UITableViewDataSource
//显示搜索结果
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tips.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tipCellIdentifier = @"tipCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tipCellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:tipCellIdentifier];
cell.imageView.image = [UIImage imageNamed:@"locate"];
}
AMapTip *tip = self.tips[indexPath.row];
if (tip.location == nil)
{
cell.imageView.image = [UIImage imageNamed:@"search"];
}
cell.textLabel.text = tip.name;
cell.detailTextLabel.text = tip.address;
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//搜索出来的地址
AMapTip *tip = self.tips[indexPath.row];
self.searchController.active = NO;
if (self.searchTipCallBack) {
self.searchTipCallBack(tip);
}
[self back];
}
@end