// // XPLoginAraeViewController.m // YuMi // // Created by YuMi on 2023/6/25. // #import "XPLoginAraeViewController.h" ///view #import "XPLoginAreaTableViewCell.h" #import "LoginAreaModel.h" #import "NSObject+MJExtension.h" #import "YUMIMacroUitls.h" #import "Api+Login.h" ///Third #import @interface XPLoginAraeViewController () @property (nonatomic,strong) UITableView *tableView; @property (nonatomic,strong) NSMutableArray *listData; @property (nonatomic,strong) NSMutableArray *titleList; @end @implementation XPLoginAraeViewController - (void)viewDidLoad { [super viewDidLoad]; [self initSubViews]; [self initSubViewConstraints]; [self getPhoneAreaCodeList]; } #pragma mark - LoginVerifCodeProtocol -(void)getPhoneAreaCodeList{ // 获取文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"pi_area_info" ofType:@"json"]; // 将文件数据化 NSData *data = [[NSData alloc] initWithContentsOfFile:path]; // 对数据进行JSON格式化并返回字典形式 NSDictionary *codeData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSArray *codeList = [LoginAreaModel modelsWithArray:codeData[@"RECORDS"]]; [self getLocalPlistWithList:codeList]; } -(void)getLocalPlistWithList:(NSArray *)list{ NSArray * letterList = @[@"",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]; NSMutableArray *otherList = [NSMutableArray array]; NSMutableArray *defaultList = [NSMutableArray array]; LoginAreaModel *TwModel; LoginAreaModel *HKModel ; LoginAreaModel *SPModel ; LoginAreaModel *MYModel ; LoginAreaModel *ChinsModel ; [self.titleList addObject:@""]; for (NSString *letter in letterList) { NSMutableArray *modelList = [NSMutableArray array]; for (LoginAreaModel *model in list) { if([model.code isEqualToString:@"886"]){ TwModel = model; } if([model.code isEqualToString:@"852"]){ HKModel = model; } if([model.code isEqualToString:@"65"]){ SPModel = model; } if([model.code isEqualToString:@"60"]){ MYModel = model; } if([model.code isEqualToString:@"86"]){ ChinsModel = model; } NSString *fristLetter = [model.name substringWithRange:NSMakeRange(0, 1)]; if([letter isEqualToString:fristLetter]){ [modelList addObject:model]; }else{ if(![letterList containsObject:fristLetter] && ![otherList containsObject:model]){ [otherList addObject:model]; } } } if(modelList.count > 0){ [self.titleList addObject:letter]; [self.listData addObject:modelList]; } } if(TwModel != nil){ [defaultList addObject:TwModel]; } if(HKModel != nil){ [defaultList addObject:HKModel]; } if(SPModel != nil){ [defaultList addObject:SPModel]; } if(MYModel != nil){ [defaultList addObject:MYModel]; } if(ChinsModel != nil){ [defaultList addObject:ChinsModel]; } [self.listData insertObject:defaultList atIndex:0]; if(otherList.count > 0){ [self.listData addObject:otherList]; [self.titleList addObject:@"#"]; } [self.tableView reloadData]; } #pragma mark - UITableViewDelegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.listData.count; } - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return self.titleList[section]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.listData[section] count]; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return self.titleList; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ XPLoginAreaTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPLoginAreaTableViewCell class]) forIndexPath:indexPath]; if(cell.delegate == nil){ cell.delegate = self; } if(indexPath.section < self.listData.count){ NSArray *listModel = self.listData[indexPath.section]; if(indexPath.row < listModel.count){ cell.areaModel =listModel[indexPath.row]; } } return cell; } #pragma mark - XPLoginAreaTableViewCellDelegate - (void)didSelectModel:(LoginAreaModel *)model{ LoginAreaModel *codeModel = model; if(self.delegate && [self.delegate respondsToSelector:@selector(chooseAreaCodeSuccess:)]){ [self.delegate chooseAreaCodeSuccess:codeModel.code]; } [self.navigationController popViewControllerAnimated:YES]; } #pragma mark - Private Method - (void)initSubViews { self.title = YMLocalizedString(@"XPChooseRreaCodeVC0"); [self.view addSubview:self.tableView]; } - (void)initSubViewConstraints { [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view); }]; } #pragma mark - Getters And Setters - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.rowHeight = kGetScaleWidth(37); _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;; _tableView.backgroundColor = [UIColor whiteColor]; [_tableView registerClass:[XPLoginAreaTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPLoginAreaTableViewCell class])]; } return _tableView; } - (NSMutableArray *)titleList{ if (!_titleList){ _titleList = [NSMutableArray array]; } return _titleList; } - (NSMutableArray *)listData{ if (!_listData){ _listData = [NSMutableArray array]; } return _listData; } @end