YOUPAILCUpdateCityVC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // YOUPAILCUpdateCityVC.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/4/14.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "YOUPAILCUpdateCityVC.h"
  9. #import "YOUPAILCCityCell.h"
  10. #import "YOUPAILCGPSCityCell.h"
  11. #import "YOUPAILCCitySearchView.h"
  12. #import "YOUPAIUCBaseTouchTableView.h"
  13. @interface YOUPAILCUpdateCityVC ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate,UCBaseTouchTableViewDelegate>
  14. @property(nonatomic,strong)UISearchBar* youpaipsearchBar;
  15. @property(nonatomic,strong)YOUPAIUCBaseTouchTableView* youpaiptableView;
  16. @property(nonatomic,strong)YOUPAILCCitySearchView* youpaipresultView;
  17. @property(nonatomic,strong)NSMutableArray* youpaipcityArray;
  18. @property(nonatomic,strong)NSMutableArray* youpaipsectionArray;
  19. @property(nonatomic,strong)NSString* youpaipgpsCity;
  20. @end
  21. @implementation YOUPAILCUpdateCityVC
  22. - (YOUPAIUCBaseTouchTableView *)youpaiptableView{
  23. if (!_youpaiptableView) {
  24. _youpaiptableView = [[YOUPAIUCBaseTouchTableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight) style:UITableViewStylePlain];
  25. _youpaiptableView.touchDelegate = self;
  26. _youpaiptableView.showsVerticalScrollIndicator = NO;
  27. _youpaiptableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  28. _youpaiptableView.backgroundColor = [UIColor whiteColor];
  29. _youpaiptableView.sectionIndexColor = LZA3AABEColor;
  30. _youpaiptableView.sectionIndexBackgroundColor = [UIColor clearColor];
  31. _youpaiptableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor];
  32. _youpaiptableView.delegate = self;
  33. _youpaiptableView.dataSource = self;
  34. if (@available(iOS 15.0, *)) {
  35. _youpaiptableView.sectionHeaderTopPadding = 0;
  36. }
  37. }
  38. return _youpaiptableView;
  39. }
  40. - (void)viewWillAppear:(BOOL)animated{
  41. [super viewWillAppear:animated];
  42. [self youpaifupdateGpsCity];
  43. }
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. [self youpaifsetupNav];
  47. [self youpaifinitData];
  48. self.youpaiptableView.rowHeight = 48;
  49. [self.view addSubview:self.youpaiptableView];
  50. self.youpaipresultView = [[YOUPAILCCitySearchView alloc]initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight)];
  51. WeakSelf;
  52. self.youpaipresultView.youpaipendEditblock = ^{
  53. [weakSelf.youpaipsearchBar resignFirstResponder];
  54. };
  55. self.youpaipresultView.youpaipsearchBlock = ^(NSString *city) {
  56. weakSelf.youpaipblock(city);
  57. [weakSelf.navigationController popViewControllerAnimated:YES];
  58. };
  59. [self.view addSubview:self.youpaipresultView];
  60. self.youpaipresultView.hidden = YES;
  61. }
  62. - (void)youpaifupdateGpsCity{
  63. WeakSelf;
  64. [[LCLocationManager shareInstance]getLocationCity:^(NSString *city) {
  65. if (![city isEqualToString:@""]) {
  66. weakSelf.youpaipgpsCity = city;
  67. [weakSelf.youpaiptableView reloadData];
  68. }
  69. }];
  70. }
  71. - (void)youpaifinitData{
  72. // NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];
  73. // NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
  74. NSMutableArray* data = [NSMutableArray arrayWithArray:[LCSaveData getCityDict].allValues];
  75. self.youpaipcityArray = [NSMutableArray arrayWithArray:[LCPinYinSortHelper getCityListDataBy:data]];
  76. self.youpaipsectionArray = [NSMutableArray arrayWithArray:[LCPinYinSortHelper getCityListSectionBy:self.youpaipcityArray]];
  77. }
  78. - (void)youpaifsetupNav{
  79. UISearchBar* search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth-105, 36)];
  80. [search setImage:[UIImage imageNamed:@"vqu_images_H_search_icon"]
  81. forSearchBarIcon:UISearchBarIconSearch
  82. state:UIControlStateNormal];
  83. // search.placeholder = @"搜索城市名或拼音";
  84. UITextField *searchField = [search valueForKey:@"searchField"];
  85. if (searchField) {
  86. [searchField setBackgroundColor:LZF7F8FAColor];
  87. searchField.layer.cornerRadius = 18.0f;
  88. searchField.layer.masksToBounds = YES;
  89. searchField.font = LCFont14;
  90. NSMutableAttributedString* placeholder = [[NSMutableAttributedString alloc]initWithString:@"搜索城市名或拼音"];
  91. [placeholder addAttribute:NSFontAttributeName value:LCFont(14) range:NSMakeRange(0, placeholder.length)];
  92. [placeholder addAttribute:NSForegroundColorAttributeName value:LZD3D1D7Color range:NSMakeRange(0, placeholder.length)];
  93. searchField.attributedPlaceholder = placeholder;
  94. }
  95. search.tintColor = LZ7C69FEColor;
  96. search.delegate = self;
  97. searchField.textColor = LZ273145Color;
  98. self.youpaipsearchBar = search;
  99. UIBarButtonItem* item = [[UIBarButtonItem alloc]initWithCustomView:search];
  100. UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  101. negativeSpacer.width = 5;
  102. self.navigationItem.rightBarButtonItems = @[negativeSpacer,item];
  103. }
  104. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  105. return self.youpaipcityArray.count;
  106. }
  107. - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  108. UIView* sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 20)];
  109. sectionView.backgroundColor = [UIColor whiteColor];
  110. UILabel* showLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0,100, 20)];
  111. showLabel.textAlignment = NSTextAlignmentLeft;
  112. [sectionView addSubview:showLabel];
  113. if (section==0) {
  114. showLabel.font = LCFont12;
  115. showLabel.textColor = LZA3AABEColor;
  116. sectionView.frame = CGRectMake(0, 0, KScreenWidth, 48);
  117. sectionView.backgroundColor = [UIColor whiteColor];
  118. showLabel.frame = CGRectMake(15, 20, 100, 20);
  119. UIButton *gpsBtn = [[UIButton alloc]init];
  120. gpsBtn.titleLabel.font = LCFont12;
  121. [gpsBtn setTitleColor:LZA3AABEColor forState:(UIControlStateNormal)];
  122. // [gpsBtn setTitle:@"获取定位" forState:(UIControlStateNormal)];
  123. [gpsBtn addTarget:self action:@selector(youpaifgpsBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
  124. gpsBtn.uxy_acceptEventInterval = 3.0;
  125. [gpsBtn setImage:[UIImage imageNamed:@"vqu_images_ic_profile_address"] forState:UIControlStateNormal];
  126. [sectionView addSubview:gpsBtn];
  127. [gpsBtn makeConstraints:^(MASConstraintMaker *make) {
  128. make.right.offset(-15);
  129. make.bottom.offset(0);
  130. make.top.offset(20.0f);
  131. }];
  132. if (self.youpaipgpsCity) {
  133. showLabel.text = [NSString stringWithFormat:@"%@%@", @"当前定位:",self.youpaipgpsCity];
  134. [gpsBtn setTitle:@"重新定位" forState:UIControlStateNormal];
  135. }else{
  136. showLabel.text = @"无法获取定位城市";
  137. [gpsBtn setTitle:@"获取定位" forState:UIControlStateNormal];
  138. }
  139. [showLabel sizeToFit];
  140. showLabel.frame = CGRectMake(15, 20, showLabel.mj_w, 20);
  141. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(youpaifcurrentCityClick)];
  142. [sectionView addGestureRecognizer:tap];
  143. }else{
  144. showLabel.font = LCFont16;
  145. showLabel.textColor = LZA3AABEColor;
  146. showLabel.text = self.youpaipsectionArray[section];
  147. }
  148. return sectionView;
  149. }
  150. - (void)youpaifgpsBtnClick{
  151. WeakSelf;
  152. [[LCLocationManager shareInstance]getLocationCity:^(NSString *city) {
  153. if (![city isEqualToString:@""]) {
  154. weakSelf.youpaipgpsCity = city;
  155. [weakSelf.youpaiptableView reloadData];
  156. }
  157. }];
  158. }
  159. - (void)youpaifcurrentCityClick{
  160. for (NSString* city in [[LCSaveData getCityDict] allValues]) {
  161. if ([self.youpaipgpsCity containsString:city]) {
  162. self.youpaipblock(city);
  163. [self.navigationController popViewControllerAnimated:YES];
  164. break;
  165. }
  166. }
  167. }
  168. - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  169. return self.youpaipsectionArray;
  170. }
  171. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  172. if (section==0) {
  173. return 48;
  174. }else{
  175. return 20;
  176. }
  177. }
  178. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  179. if (section==0) {
  180. return 0;
  181. }else{
  182. return [self.youpaipcityArray[section-1] count];
  183. }
  184. }
  185. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  186. if (indexPath.section==0) {
  187. YOUPAILCGPSCityCell *cell = [[YOUPAILCGPSCityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YOUPAILCGPSCityCell"];
  188. if (self.youpaipgpsCity) {
  189. cell.youpaipcityLabel.text = self.youpaipgpsCity;
  190. [cell.youpaipgpsBtn setTitle:@"重新定位" forState:UIControlStateNormal];
  191. }else{
  192. cell.youpaipcityLabel.text = @"无法获取定位城市";
  193. [cell.youpaipgpsBtn setTitle:@"获取定位" forState:UIControlStateNormal];
  194. }
  195. WeakSelf;
  196. cell.youpaipblock = ^{
  197. [[LCLocationManager shareInstance]getLocationCity:^(NSString *city) {
  198. if (![city isEqualToString:@""]) {
  199. weakSelf.youpaipgpsCity = city;
  200. [weakSelf.youpaiptableView reloadData];
  201. }
  202. }];
  203. };
  204. return cell;
  205. }else{
  206. YOUPAILCCityCell *cell = [[YOUPAILCCityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YOUPAILCCityCell"];
  207. cell.youpaipcityLabel.text = self.youpaipcityArray[indexPath.section-1][indexPath.row];
  208. [cell addLineWithColor:LZF5F4F7Color lineRect:CGRectMake(15.0f, 47.5f, KScreenWidth - 30.0f, 0.5f)];
  209. return cell;
  210. }
  211. }
  212. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  213. if (indexPath.section==0) {
  214. if (self.youpaipgpsCity) {
  215. for (NSString* city in [[LCSaveData getCityDict] allValues]) {
  216. if ([self.youpaipgpsCity containsString:city]) {
  217. self.youpaipblock(city);
  218. [self.navigationController popViewControllerAnimated:YES];
  219. break;
  220. }
  221. }
  222. }else{
  223. [ZCHUDHelper showTitle:@"暂时无法获取定位城市"];
  224. }
  225. }else{
  226. NSString* city = self.youpaipcityArray[indexPath.section-1][indexPath.row];
  227. self.youpaipblock(city);
  228. [self.navigationController popViewControllerAnimated:YES];
  229. }
  230. }
  231. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  232. NSLog(@"%@",searchText);
  233. NSLog(@"%@",searchBar.text);
  234. if ([searchBar.text isEqualToString:@""]) {
  235. self.youpaiptableView.hidden = NO;
  236. self.youpaipresultView.hidden = YES;
  237. }else{
  238. self.youpaiptableView.hidden = YES;
  239. self.youpaipresultView.hidden = NO;
  240. [self.youpaipresultView youpaifupdateWithString:searchBar.text];
  241. }
  242. }
  243. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  244. [self.youpaipsearchBar resignFirstResponder];
  245. }
  246. - (void)youpaifdidTouchesBegan:(YOUPAIUCBaseTouchTableView *)tableView{
  247. [self.youpaipsearchBar resignFirstResponder];
  248. }
  249. @end