SearchPOIListViewController.m 6.01 KB
//
//  SearchPOIListViewController.m
//  IM_ios_client
//
//  Created by zsq on 2018/9/6.
//  Copyright © 2018年 xiniu. All rights reserved.
//

#import "SearchPOIListViewController.h"

#import "YYSearchView.h"

@interface SearchPOIListViewController ()<AMapSearchDelegate>

{
    YYSearchView * searchView;
    UIView *v_top;
    UITableView *tab_result;
    UIView *noResultView;
    NSMutableArray *muarr_list;
    
}

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) AMapSearchAPI        *search;

@end

@implementation SearchPOIListViewController

- (void)dealloc{
    self.search.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    muarr_list = [[NSMutableArray alloc]init];
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    
    v_top = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, TOP_OFFSET)];
    v_top.backgroundColor = COLOR_C_f4f4f4;
    searchView = [YYSearchView creatView];
    searchView.frame = CGRectMake(0, kStatusBarHeight, ScreenWidth, 44);
    searchView.YYSearch.delegate = self;
    searchView.YYSearch.returnKeyType = UIReturnKeySearch;
    [searchView.YYSearch becomeFirstResponder];
    __weak __typeof(self) weakSelf = self;
    [searchView setYYGetCancel:^{
        [weakSelf dismissViewControllerAnimated:NO completion:nil];
    }];
    [searchView setYYGetTitle:^(NSString * title)
     {
       NSString *trimedString = [CommonFun deleteWhitespaceNewline:title type:0];
         [weakSelf searchKeyWords:trimedString];
     }];
    
    [v_top addSubview:searchView];
    
    [self.mainLayout addSubview:v_top];
    
    tab_result = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tab_result.estimatedSectionHeaderHeight = 0;
    tab_result.delegate = self;
    tab_result.dataSource = self;
    [self.mainLayout addSubview:tab_result];
    [tab_result mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->v_top.mas_bottom);
        make.left.right.bottom.equalTo(self.mainLayout);
    }];
    
    noResultView = [[UIView alloc]init];
    noResultView.backgroundColor = COLOR_C_FRom(0xffffff, 1.0);
    [self.mainLayout addSubview:noResultView];
    [noResultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->v_top.mas_bottom);
        make.left.right.bottom.equalTo(self.mainLayout);
    }];
    
    UIImageView *img_nosearch = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"搜索无结果"]];
    [noResultView addSubview:img_nosearch];
    [img_nosearch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->noResultView);
        make.top.equalTo(self->noResultView.mas_top).offset(140);
    }];
    
    UILabel *lbl_title = [[UILabel alloc]init];
    lbl_title.textColor = UIColorFromRGB(0x888888, 1.0);
    lbl_title.text = @"暂无结果,换个词试试吧~";
    lbl_title.font =  [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    [noResultView addSubview:lbl_title];
    [lbl_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->noResultView);
        make.top.equalTo(img_nosearch.mas_bottom).offset(12);
    }];
    
    noResultView.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)searchKeyWords:(NSString *)title
{
    if (title.length <=0 ) {
        [muarr_list removeAllObjects];
        [tab_result reloadData];
        return;
    }
    
    AMapInputTipsSearchRequest *tips = [[AMapInputTipsSearchRequest alloc] init];
    tips.keywords = title;
    tips.city     = self.cityAddress.city;
    tips.cityLimit = NO;
    [self.search AMapInputTipsSearch:tips];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.text.length <=0 ) {
        [muarr_list removeAllObjects];
        [tab_result reloadData];
        return YES;
    }
    AMapInputTipsSearchRequest *tips = [[AMapInputTipsSearchRequest alloc] init];
    tips.keywords = textField.text;
    tips.city     = self.cityAddress.city;
    tips.cityLimit = NO;
    [self.search AMapInputTipsSearch:tips];
    return YES;
}

- (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response
{
    //解析response获取提示词,具体解析见 Demo
    if (response.tips.count>0) {
        muarr_list = [response.tips mutableCopy];
        [self.view bringSubviewToFront:tab_result];
        noResultView.hidden = YES;
    }else{
        [self.view bringSubviewToFront:noResultView];
        noResultView.hidden = NO;
        [muarr_list removeAllObjects];
    }
    [tab_result reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return muarr_list.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    AMapTip *tip = muarr_list[indexPath.row];
    
    cell.textLabel.text = tip.name;
    cell.detailTextLabel.text = tip.address;
    cell.detailTextLabel.textColor = UIColorFromRGB(0x999999, 1.0);
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [searchView.YYSearch resignFirstResponder];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    AMapTip *tip = muarr_list[indexPath.row];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"noti_reviewCenter" object:tip];
    
    [self dismissViewControllerAnimated:NO completion:nil];
}

@end