// // 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]; [Api getPhoneAreaCodeList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { if (code == 200) { NSArray *list = [LoginAreaModel modelsWithArray:data.data]; [self.titleList removeAllObjects]; [self.listData removeAllObjects]; [self getLocalPlistWithList:list]; } }]; } #pragma mark - LoginVerifCodeProtocol -(void)getPhoneAreaCodeListSuccess:(NSArray *)list{ [self hideHUD]; [self.titleList removeAllObjects]; [self.listData removeAllObjects]; [self getLocalPlistWithList:list]; } -(void)getPhoneAreaCodeListFail{ [self hideHUD]; NSBundle *bundle = [NSBundle mainBundle]; NSString *file = [bundle pathForResource:@"district" ofType:@"plist"]; NSArray *dictArray = [NSArray arrayWithContentsOfFile:file]; NSArray *list = [LoginAreaModel modelsWithArray:dictArray]; [self getLocalPlistWithList:list]; } -(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]; for (NSString *letter in letterList) { NSMutableArray *modelList = [NSMutableArray array]; for (LoginAreaModel *model in list) { 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]; } } [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(indexPath.section < self.listData.count){ NSArray *listModel = self.listData[indexPath.section]; if(indexPath.row < listModel.count){ cell.areaModel =listModel[indexPath.row]; } } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ LoginAreaModel *codeModel = self.listData[indexPath.section][indexPath.row]; if(self.delegate && [self.delegate respondsToSelector:@selector(chooseAreaCodeSuccess:)]){ [self.delegate chooseAreaCodeSuccess:codeModel.pi_phoneAreaCode ?: codeModel.phone_area_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