// // YOUPAILCUpdateCityVC.m // LiveChat // // Created by 张灿 on 2018/4/14. // Copyright © 2018年 caicai. All rights reserved. // #import "YOUPAILCUpdateCityVC.h" #import "YOUPAILCCityCell.h" #import "YOUPAILCGPSCityCell.h" #import "YOUPAILCCitySearchView.h" #import "YOUPAIUCBaseTouchTableView.h" @interface YOUPAILCUpdateCityVC () @property(nonatomic,strong)UISearchBar* youpaipsearchBar; @property(nonatomic,strong)YOUPAIUCBaseTouchTableView* youpaiptableView; @property(nonatomic,strong)YOUPAILCCitySearchView* youpaipresultView; @property(nonatomic,strong)NSMutableArray* youpaipcityArray; @property(nonatomic,strong)NSMutableArray* youpaipsectionArray; @property(nonatomic,strong)NSString* youpaipgpsCity; @end @implementation YOUPAILCUpdateCityVC - (YOUPAIUCBaseTouchTableView *)youpaiptableView{ if (!_youpaiptableView) { _youpaiptableView = [[YOUPAIUCBaseTouchTableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight) style:UITableViewStylePlain]; _youpaiptableView.touchDelegate = self; _youpaiptableView.showsVerticalScrollIndicator = NO; _youpaiptableView.separatorStyle = UITableViewCellSeparatorStyleNone; _youpaiptableView.backgroundColor = [UIColor whiteColor]; _youpaiptableView.sectionIndexColor = LZA3AABEColor; _youpaiptableView.sectionIndexBackgroundColor = [UIColor clearColor]; _youpaiptableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor]; _youpaiptableView.delegate = self; _youpaiptableView.dataSource = self; if (@available(iOS 15.0, *)) { _youpaiptableView.sectionHeaderTopPadding = 0; } } return _youpaiptableView; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self youpaifupdateGpsCity]; } - (void)viewDidLoad { [super viewDidLoad]; [self youpaifsetupNav]; [self youpaifinitData]; self.youpaiptableView.rowHeight = 48; [self.view addSubview:self.youpaiptableView]; self.youpaipresultView = [[YOUPAILCCitySearchView alloc]initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight)]; WeakSelf; self.youpaipresultView.youpaipendEditblock = ^{ [weakSelf.youpaipsearchBar resignFirstResponder]; }; self.youpaipresultView.youpaipsearchBlock = ^(NSString *city) { weakSelf.youpaipblock(city); [weakSelf.navigationController popViewControllerAnimated:YES]; }; [self.view addSubview:self.youpaipresultView]; self.youpaipresultView.hidden = YES; } - (void)youpaifupdateGpsCity{ WeakSelf; [[LCLocationManager shareInstance]getLocationCity:^(NSString *city) { if (![city isEqualToString:@""]) { weakSelf.youpaipgpsCity = city; [weakSelf.youpaiptableView reloadData]; } }]; } - (void)youpaifinitData{ // NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"]; // NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath]; NSMutableArray* data = [NSMutableArray arrayWithArray:[LCSaveData getCityDict].allValues]; self.youpaipcityArray = [NSMutableArray arrayWithArray:[LCPinYinSortHelper getCityListDataBy:data]]; self.youpaipsectionArray = [NSMutableArray arrayWithArray:[LCPinYinSortHelper getCityListSectionBy:self.youpaipcityArray]]; } - (void)youpaifsetupNav{ UISearchBar* search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth-105, 36)]; [search setImage:[UIImage imageNamed:@"vqu_images_H_search_icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; // search.placeholder = @"搜索城市名或拼音"; UITextField *searchField = [search valueForKey:@"searchField"]; if (searchField) { [searchField setBackgroundColor:LZF7F8FAColor]; searchField.layer.cornerRadius = 18.0f; searchField.layer.masksToBounds = YES; searchField.font = LCFont14; NSMutableAttributedString* placeholder = [[NSMutableAttributedString alloc]initWithString:@"搜索城市名或拼音"]; [placeholder addAttribute:NSFontAttributeName value:LCFont(14) range:NSMakeRange(0, placeholder.length)]; [placeholder addAttribute:NSForegroundColorAttributeName value:LZD3D1D7Color range:NSMakeRange(0, placeholder.length)]; searchField.attributedPlaceholder = placeholder; } search.tintColor = LZ7C69FEColor; search.delegate = self; searchField.textColor = LZ273145Color; self.youpaipsearchBar = search; UIBarButtonItem* item = [[UIBarButtonItem alloc]initWithCustomView:search]; UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = 5; self.navigationItem.rightBarButtonItems = @[negativeSpacer,item]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.youpaipcityArray.count; } - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView* sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 20)]; sectionView.backgroundColor = [UIColor whiteColor]; UILabel* showLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0,100, 20)]; showLabel.textAlignment = NSTextAlignmentLeft; [sectionView addSubview:showLabel]; if (section==0) { showLabel.font = LCFont12; showLabel.textColor = LZA3AABEColor; sectionView.frame = CGRectMake(0, 0, KScreenWidth, 48); sectionView.backgroundColor = [UIColor whiteColor]; showLabel.frame = CGRectMake(15, 20, 100, 20); UIButton *gpsBtn = [[UIButton alloc]init]; gpsBtn.titleLabel.font = LCFont12; [gpsBtn setTitleColor:LZA3AABEColor forState:(UIControlStateNormal)]; // [gpsBtn setTitle:@"获取定位" forState:(UIControlStateNormal)]; [gpsBtn addTarget:self action:@selector(youpaifgpsBtnClick) forControlEvents:(UIControlEventTouchUpInside)]; gpsBtn.uxy_acceptEventInterval = 3.0; [gpsBtn setImage:[UIImage imageNamed:@"vqu_images_ic_profile_address"] forState:UIControlStateNormal]; [sectionView addSubview:gpsBtn]; [gpsBtn makeConstraints:^(MASConstraintMaker *make) { make.right.offset(-15); make.bottom.offset(0); make.top.offset(20.0f); }]; if (self.youpaipgpsCity) { showLabel.text = [NSString stringWithFormat:@"%@%@", @"当前定位:",self.youpaipgpsCity]; [gpsBtn setTitle:@"重新定位" forState:UIControlStateNormal]; }else{ showLabel.text = @"无法获取定位城市"; [gpsBtn setTitle:@"获取定位" forState:UIControlStateNormal]; } [showLabel sizeToFit]; showLabel.frame = CGRectMake(15, 20, showLabel.mj_w, 20); UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(youpaifcurrentCityClick)]; [sectionView addGestureRecognizer:tap]; }else{ showLabel.font = LCFont16; showLabel.textColor = LZA3AABEColor; showLabel.text = self.youpaipsectionArray[section]; } return sectionView; } - (void)youpaifgpsBtnClick{ WeakSelf; [[LCLocationManager shareInstance]getLocationCity:^(NSString *city) { if (![city isEqualToString:@""]) { weakSelf.youpaipgpsCity = city; [weakSelf.youpaiptableView reloadData]; } }]; } - (void)youpaifcurrentCityClick{ for (NSString* city in [[LCSaveData getCityDict] allValues]) { if ([self.youpaipgpsCity containsString:city]) { self.youpaipblock(city); [self.navigationController popViewControllerAnimated:YES]; break; } } } - (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return self.youpaipsectionArray; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section==0) { return 48; }else{ return 20; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section==0) { return 0; }else{ return [self.youpaipcityArray[section-1] count]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section==0) { YOUPAILCGPSCityCell *cell = [[YOUPAILCGPSCityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YOUPAILCGPSCityCell"]; if (self.youpaipgpsCity) { cell.youpaipcityLabel.text = self.youpaipgpsCity; [cell.youpaipgpsBtn setTitle:@"重新定位" forState:UIControlStateNormal]; }else{ cell.youpaipcityLabel.text = @"无法获取定位城市"; [cell.youpaipgpsBtn setTitle:@"获取定位" forState:UIControlStateNormal]; } WeakSelf; cell.youpaipblock = ^{ [[LCLocationManager shareInstance]getLocationCity:^(NSString *city) { if (![city isEqualToString:@""]) { weakSelf.youpaipgpsCity = city; [weakSelf.youpaiptableView reloadData]; } }]; }; return cell; }else{ YOUPAILCCityCell *cell = [[YOUPAILCCityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YOUPAILCCityCell"]; cell.youpaipcityLabel.text = self.youpaipcityArray[indexPath.section-1][indexPath.row]; [cell addLineWithColor:LZF5F4F7Color lineRect:CGRectMake(15.0f, 47.5f, KScreenWidth - 30.0f, 0.5f)]; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section==0) { if (self.youpaipgpsCity) { for (NSString* city in [[LCSaveData getCityDict] allValues]) { if ([self.youpaipgpsCity containsString:city]) { self.youpaipblock(city); [self.navigationController popViewControllerAnimated:YES]; break; } } }else{ [ZCHUDHelper showTitle:@"暂时无法获取定位城市"]; } }else{ NSString* city = self.youpaipcityArray[indexPath.section-1][indexPath.row]; self.youpaipblock(city); [self.navigationController popViewControllerAnimated:YES]; } } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ NSLog(@"%@",searchText); NSLog(@"%@",searchBar.text); if ([searchBar.text isEqualToString:@""]) { self.youpaiptableView.hidden = NO; self.youpaipresultView.hidden = YES; }else{ self.youpaiptableView.hidden = YES; self.youpaipresultView.hidden = NO; [self.youpaipresultView youpaifupdateWithString:searchBar.text]; } } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ [self.youpaipsearchBar resignFirstResponder]; } - (void)youpaifdidTouchesBegan:(YOUPAIUCBaseTouchTableView *)tableView{ [self.youpaipsearchBar resignFirstResponder]; } @end