CountryCodeVC.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // CountryCodeVC.m
  3. // yunbaolive
  4. //
  5. // Created by ybRRR on 2020/11/11.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "CountryCodeVC.h"
  9. #import "YBSearchBarView.h"
  10. @interface CountryCodeVC ()<UITableViewDelegate,UITableViewDataSource>
  11. {
  12. BOOL _isSearch;
  13. }
  14. @property (nonatomic, strong)NSArray *listArray;
  15. @property (nonatomic, strong)UITableView *listTable;
  16. @property (nonatomic, strong)UITableView *searchTableView;
  17. @property (nonatomic, strong)NSMutableArray *sortedArrForArrays;
  18. @property (nonatomic, strong) NSMutableArray *sectionHeadsKeys;
  19. @property (nonatomic, strong) NSArray *searchArr;
  20. @property (nonatomic, strong) YBSearchBarView *searchView;
  21. @end
  22. @implementation CountryCodeVC
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.titleL.text = YZMsg(@"手机号归属地");
  26. _sortedArrForArrays = [NSMutableArray array];
  27. _sectionHeadsKeys = [[NSMutableArray alloc] init]; //initialize a array to hold keys like A,B,C ...
  28. _searchArr = [NSArray array];
  29. [self addSearchBar];
  30. _listArray = [NSArray array];
  31. _listTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64+statusbarHeight+58, _window_width, _window_height-64-statusbarHeight-58)];
  32. _listTable.delegate = self;
  33. _listTable.dataSource = self;
  34. _listTable.tableFooterView = [[UIView alloc] init];
  35. _listTable.sectionIndexColor = [UIColor grayColor];
  36. _listTable.backgroundColor = Normal_BackColor;
  37. [_listTable setSeparatorColor:Line_Cor];
  38. [self.view addSubview:_listTable];
  39. _searchTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64+statusbarHeight+58, _window_width, _window_height-64-statusbarHeight-58)];
  40. _searchTableView.delegate = self;
  41. _searchTableView.dataSource = self;
  42. _searchTableView.tableFooterView = [[UIView alloc] init];
  43. _searchTableView.backgroundColor = Normal_BackColor;
  44. [_searchTableView setSeparatorColor:Line_Cor];
  45. [self.view addSubview:_searchTableView];
  46. _searchTableView.hidden = YES;
  47. [self requstDatalist];
  48. }
  49. #pragma mark - 输入框
  50. - (void)addSearchBar {
  51. YBWeakSelf;
  52. UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(0, 64+statusbarHeight, _window_width, statusbarHeight+58)];
  53. bg.backgroundColor =Normal_BackColor;// RGBA(238,238,238,1);
  54. [self.view addSubview:bg];
  55. _searchView = [[YBSearchBarView alloc]initWithFrame:CGRectMake(0,9, _window_width,40)];
  56. _searchView.searchTF.frame = CGRectMake(26, 2, _window_width-26*2, 36);
  57. _searchView.searchTF.placeholder = YZMsg(@"请输入地区名");
  58. _searchView.searchTF.backgroundColor =Normal_SubColor;// [UIColor whiteColor];
  59. _searchView.searchTF.textColor = UIColor.whiteColor;
  60. // _searchView.cancleBtn.hidden = YES;
  61. _searchView.searchEvent = ^(RKSearchType searchType) {
  62. [weakSelf searchEevnt:searchType];
  63. };
  64. [bg addSubview:_searchView];
  65. }
  66. -(void)requstDatalist{
  67. NSDictionary *dic = @{@"field":@""};
  68. [YBNetworking postWithUrl:@"Login.getCountrys" Dic:dic Suc:^(int code, id info, NSString *msg) {
  69. if (code == 0) {
  70. NSArray *infos = info;
  71. _listArray = infos;
  72. for (NSDictionary *listdic in _listArray) {
  73. [self.sortedArrForArrays addObject:[listdic valueForKey:@"title"]];
  74. }
  75. [self.listTable reloadData];
  76. }
  77. } Fail:^(id fail) {
  78. }];
  79. }
  80. -(void)searchList:(NSString *)searchText{
  81. NSDictionary *dic = @{@"field":searchText};
  82. [YBNetworking postWithUrl:@"Login.getCountrys" Dic:dic Suc:^(int code, id info, NSString *msg) {
  83. if (code == 0) {
  84. NSArray *infos = info;
  85. _searchArr = infos;
  86. [self.searchTableView reloadData];
  87. }
  88. } Fail:^(id fail) {
  89. }];
  90. }
  91. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  92. if (tableView == _listTable) {
  93. NSArray *list = [self.listArray[section] valueForKey:@"lists"];
  94. return list.count;
  95. }else{
  96. return _searchArr.count;
  97. }
  98. }
  99. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  100. if (tableView == _listTable) {
  101. return self.listArray.count;
  102. }else{
  103. return 1;
  104. }
  105. }
  106. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  107. {
  108. UIView *headView = [[UIView alloc]init];
  109. headView.frame = CGRectMake(0, 0, _window_width, 30);
  110. headView.backgroundColor = Normal_BackColor;//[UIColor whiteColor];
  111. UILabel *titleLb = [[UILabel alloc]init];
  112. titleLb.frame = CGRectMake(12, 10, 100, 20);
  113. titleLb.font = [UIFont systemFontOfSize:14];
  114. titleLb.textColor = [UIColor whiteColor];
  115. [headView addSubview:titleLb];
  116. titleLb.text = [[self.listArray objectAtIndex:section] valueForKey:@"title"];
  117. [PublicObj lineViewWithFrame:CGRectMake(15, 29, _window_width-30, 1) andColor:Line_Cor andView:headView];
  118. return headView;
  119. }
  120. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  121. {
  122. if (tableView == _listTable) {
  123. return 30;
  124. }else{
  125. return 0;
  126. }
  127. }
  128. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  129. if (tableView == _listTable) {
  130. return self.sortedArrForArrays;
  131. }else
  132. return nil;
  133. }
  134. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  135. {
  136. if (tableView == _listTable) {
  137. UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ss"];
  138. NSArray *listArr = [[_listArray objectAtIndex:indexPath.section] valueForKey:@"lists"];
  139. cell.backgroundColor = Normal_SubColor;
  140. cell.textLabel.text = [NSString stringWithFormat:@"%@",[listArr[indexPath.row]valueForKey:@"name"]];//listArr[indexPath.row]valueForKey:@"tel"]
  141. if ([lagType isEqual:EN]) {
  142. cell.textLabel.text = [NSString stringWithFormat:@"%@",[listArr[indexPath.row]valueForKey:@"name_en"]];
  143. }
  144. cell.textLabel.font = [UIFont systemFontOfSize:12];
  145. cell.textLabel.textColor = [UIColor whiteColor];
  146. return cell;
  147. }else{
  148. UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ss"];
  149. cell.backgroundColor = Normal_SubColor;
  150. cell.textLabel.text = [NSString stringWithFormat:@"%@",[_searchArr[indexPath.row]valueForKey:@"name"]];//,[_searchArr[indexPath.row]valueForKey:@"tel"]
  151. if ([lagType isEqual:EN]) {
  152. cell.textLabel.text = [NSString stringWithFormat:@"%@",[_searchArr[indexPath.row]valueForKey:@"name_en"]];
  153. }
  154. cell.textLabel.font = [UIFont systemFontOfSize:12];
  155. cell.textLabel.textColor = [UIColor whiteColor];
  156. return cell;
  157. }
  158. }
  159. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  160. {
  161. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  162. if (tableView == _listTable) {
  163. NSArray *listArr = [[_listArray objectAtIndex:indexPath.section] valueForKey:@"lists"];
  164. if (self.codeEvent) {
  165. self.codeEvent(listArr[indexPath.row]);
  166. }
  167. }else{
  168. if (self.codeEvent) {
  169. self.codeEvent(_searchArr[indexPath.row]);
  170. }
  171. }
  172. [[YBBaseAppDelegate sharedAppDelegate]popViewController:YES];
  173. }
  174. #pragma mark - 搜索事件 s
  175. -(void)searchEevnt:(RKSearchType)searchType {
  176. if (searchType == RKSearch_Search) {
  177. //搜索
  178. [self searchList:_searchView.searchTF.text];
  179. }
  180. if (searchType == RKSearch_ValueChange) {
  181. //输入框改变
  182. NSString *allText = [_searchView.searchTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  183. if (allText.length > 0) {
  184. _searchTableView.hidden = NO;
  185. _isSearch = YES;
  186. [self searchList:_searchView.searchTF.text];
  187. }else{
  188. _isSearch = NO;
  189. _searchTableView.hidden = YES;
  190. [self searchList:_searchView.searchTF.text];
  191. }
  192. }
  193. if (searchType == RKSearch_BeginEditing) {
  194. //开始编辑
  195. if (_searchView.searchTF.text.length>0) {
  196. _isSearch = YES;
  197. _searchTableView.hidden = NO;
  198. }else{
  199. _isSearch = NO;
  200. _searchTableView.hidden = YES;
  201. }
  202. }
  203. }
  204. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  205. {
  206. [_searchView.searchTF resignFirstResponder];
  207. }
  208. @end