123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- //
- // YMCityPickerPopupView.m
- // MSYOUPAI
- //
- // Created by YoMi on 2024/3/10.
- // Copyright © 2024 MS. All rights reserved.
- //
- #import "YMCityPickerPopupView.h"
- @interface YMCityPickerPopupView ()<UIPickerViewDelegate,UIPickerViewDataSource>
- /** 标题标签*/
- @property (nonatomic, strong) UILabel *titleLb;
- /** 取消按钮*/
- @property (nonatomic, strong) UIButton *cancelBtn;
- /** 确认按钮*/
- @property (nonatomic, strong) UIButton *confirmBtn;
- /** 选择器*/
- @property (nonatomic, strong) UIPickerView *pickerView;
- /** 全部数据*/
- @property (nonatomic, strong) NSArray <YMProvinceModel*>*allArr;
- /** 省份数据*/
- @property (nonatomic, strong) NSMutableArray *provinceArr;
- /** 城市数据*/
- @property (nonatomic, strong) NSMutableArray *cityArr;
- /** 当前选中省份*/
- @property (nonatomic, strong) NSString *currentSelectProvince;
- /** 当前选中城市*/
- @property (nonatomic, strong) NSString *currentSelectCity;
- /** 当前选中省份Id*/
- @property (nonatomic, strong) NSString *currentSelectProvinceId;
- /** 当前选中城市Id*/
- @property (nonatomic, strong) NSString *currentSelectCityId;
- @end
- @implementation YMCityPickerPopupView
- - (void)ym_setupViews{
- self.frame = CGRectMake(0, 0, kFrameWidth, 250);
-
- self.backgroundColor = HexColorFromRGB(0xFFFFFF);
- self.titleText = @"请选择选项";
- self.titleColor = HexColorFromRGB(0x000000);
-
- self.titleFont = LCFont(13);
- self.cancelBgColor = [UIColor clearColor];
- self.confirmBgColor = [UIColor clearColor];
-
- self.cancelTitleColor = HexColorFromRGB(0x333333);
- self.confirmTitleColor = HexColorFromRGB(0x333333);
-
- self.cancelFont = LCFont(14);
- self.confirmFont = LCFont(14);
-
- self.cancelTitle = @"取消";
- self.confirmTitle = @"确定";
-
- self.cancelRadius = 10;
- self.confirmRadius = 10;
-
- self.cancelBorderColor = [UIColor clearColor];
- self.confirmBorderColor = [UIColor clearColor];
-
- self.cancelBorderWidth = 0;
- self.confirmBorderWidth = 0;
-
- [self getDataArray];
-
- [self addSubview:self.cancelBtn];
- [self addSubview:self.titleLb];
- [self addSubview:self.confirmBtn];
- [self addSubview:self.pickerView];
-
- [self setNeedsUpdateConstraints];
- [self updateConstraintsIfNeeded];
- }
- - (void)updateConstraints{
-
- [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(adapt(10));
- make.left.equalTo(self).offset(adapt(10));
- make.width.mas_equalTo(adapt(60));
- make.height.mas_equalTo(adapt(30));
- }];
-
- [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(adapt(10));
- make.left.equalTo(self.cancelBtn.mas_right).offset(adapt(10));
- make.right.equalTo(self.confirmBtn.mas_left).offset(adapt(-10));
- make.height.mas_equalTo(adapt(30));
- }];
-
- [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(adapt(10));
- make.right.equalTo(self).offset(adapt(-10));
- make.width.mas_equalTo(adapt(60));
- make.height.mas_equalTo(adapt(30));
- }];
-
- [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.titleLb.mas_bottom);
- make.left.equalTo(self);
- make.right.equalTo(self);
- make.bottom.equalTo(self).offset(adapt(-15));
- }];
-
- [super updateConstraints];
- }
- - (void)getDataArray {
- [self.provinceArr removeAllObjects];
- [self.cityArr removeAllObjects];
-
- NSArray *provinceCityArr = [OCUserDefaults objectForKey:kPROVINCE_CITY_ARR];
- if (!OCArrayIsEmpty(provinceCityArr)) {
- self.allArr = [NSArray yy_modelArrayWithClass:[YMProvinceModel class] json:provinceCityArr];
- } else if (!OCArrayIsEmpty([YMGlobalUtils shared].provinceCityArr)) {
- self.allArr = [NSArray yy_modelArrayWithClass:[YMProvinceModel class] json:[YMGlobalUtils shared].provinceCityArr];
- } else {
- [ZCHUDHelper showWithStatus:@"正在获取省市级数据"];
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- [[YMGlobalUtils shared] requestProvinceCity];
- [[YMGlobalUtils shared] requestCityCode];
- dispatch_async(dispatch_get_main_queue(), ^{
- [ZCHUDHelper dismiss];
- });
- });
- return;
- }
-
- [self calculationCityAreaArr];
-
- [self.pickerView selectRow:[self.provinceArr indexOfObject:self.currentSelectProvince] inComponent:0 animated:YES];
- [self.pickerView selectRow:[self.cityArr indexOfObject:self.currentSelectCity] inComponent:1 animated:YES];
- }
- /// 计算当前市区数组
- - (void)calculationCityAreaArr {
- [self.provinceArr removeAllObjects];
- [self.cityArr removeAllObjects];
-
- if (!self.currentSelectProvince) {
- self.currentSelectProvince = self.allArr[0].provinceName;
- }
- for (YMProvinceModel *province in self.allArr) {
- [self.provinceArr addObject:province.provinceName];
- if ([self.currentSelectProvince isEqualToString:province.provinceName]) {
- if (!self.currentSelectCity) {
- self.currentSelectCity = province.cityArr[0].cityName;
- }
- for (YMCityModel *city in province.cityArr) {
- [self.cityArr addObject:city.cityName];
- if ([city.cityName isEqualToString:self.currentSelectCity]) {
-
- }
- }
- }
- }
- [self.pickerView reloadAllComponents];
- }
- //返回UIPickerView中Component列的宽度
- //- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
- // return 50;
- //}
-
- //返回UIPickerView中Component列中每行的高度
- - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
- return 50;
- }
- //该方法返回值决定该控件包含多少列
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
- return 2;
- }
- //该方法的返回值决定该控件指定列包含多少个列表项
- - (NSInteger)pickerView:(UIPickerView *)pickerView
- numberOfRowsInComponent:(NSInteger)component{
- if (component == 0) {
- return self.provinceArr.count;
- }else {
- return self.cityArr.count;
- }
- }
- // UIPickerView中指定列和列表项上显示的标题
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
- (NSInteger)row forComponent:(NSInteger)component{
- if (component == 0) {
- return self.provinceArr[row];
- }else{
- return self.cityArr[row];
- }
- }
- // 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
- if (component == 0) {
- self.currentSelectProvince = self.provinceArr[row];
- self.currentSelectProvinceId = [self.allArr objectAtIndex:[self.provinceArr indexOfObject:self.currentSelectProvince]].provinceId;
- self.currentSelectCity = nil;
- [self calculationCityAreaArr];
- [pickerView selectRow:[self.cityArr indexOfObject:self.currentSelectCity] inComponent:1 animated:YES];
- }else if (component == 1) {
- self.currentSelectCity = self.cityArr[row];
- self.currentSelectCityId = [[self.allArr objectAtIndex:[self.provinceArr indexOfObject:self.currentSelectProvince]].cityArr objectAtIndex:[self.cityArr indexOfObject:self.currentSelectCity]].cityId;
- [self calculationCityAreaArr];
- }
- }
- - (void)setTitleText:(NSString *)titleText{
- _titleText = titleText;
- self.titleLb.text = titleText;
- }
- - (void)setCancelBgColor:(UIColor *)cancelBgColor{
- _cancelBgColor = cancelBgColor;
- _cancelBtn.backgroundColor = cancelBgColor;
- }
- - (void)setConfirmBgColor:(UIColor *)confirmBgColor{
- _confirmBgColor = confirmBgColor;
- self.confirmBtn.backgroundColor = confirmBgColor;
- }
- - (void)setCancelTitleColor:(UIColor *)cancelTitleColor{
- _cancelTitleColor = cancelTitleColor;
- [self.cancelBtn setTitleColor:cancelTitleColor forState:UIControlStateNormal];
- }
- - (void)setConfirmTitleColor:(UIColor *)confirmTitleColor{
- _confirmTitleColor = confirmTitleColor;
- [self.confirmBtn setTitleColor:confirmTitleColor forState:UIControlStateNormal];
- }
- - (void)setCancelFont:(UIFont *)cancelFont{
- _cancelFont = cancelFont;
- self.cancelBtn.titleLabel.font = cancelFont;
- }
- - (void)setConfirmFont:(UIFont *)confirmFont{
- _confirmFont = confirmFont;
- self.confirmBtn.titleLabel.font = confirmFont;
- }
- - (void)setCancelTitle:(NSString *)cancelTitle{
- _cancelTitle = cancelTitle;
- [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
- }
- - (void)setConfirmTitle:(NSString *)confirmTitle{
- _confirmTitle = confirmTitle;
- [self.confirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
- }
- - (void)setCancelRadius:(CGFloat)cancelRadius{
- _cancelRadius = cancelRadius;
- self.cancelBtn.layer.cornerRadius = cancelRadius;
- }
- - (void)setConfirmRadius:(CGFloat)confirmRadius{
- _confirmRadius = confirmRadius;
- self.confirmBtn.layer.cornerRadius = confirmRadius;
- }
- - (void)setCancelBorderColor:(UIColor *)cancelBorderColor{
- _cancelBorderColor = cancelBorderColor;
- self.cancelBtn.layer.borderColor = cancelBorderColor.CGColor;
- }
- - (void)setConfirmBorderColor:(UIColor *)confirmBorderColor{
- _confirmBorderColor = confirmBorderColor;
- self.confirmBtn.layer.borderColor = confirmBorderColor.CGColor;
- }
- - (void)setCancelBorderWidth:(CGFloat)cancelBorderWidth{
- _cancelBorderWidth = cancelBorderWidth;
- self.cancelBtn.layer.borderWidth = cancelBorderWidth;
- }
- - (void)setConfirmBorderWidth:(CGFloat)confirmBorderWidth{
- _confirmBorderWidth = confirmBorderWidth;
- self.confirmBtn.layer.borderWidth = confirmBorderWidth;
- }
- - (UILabel *)titleLb{
- if (!_titleLb) {
- _titleLb = [[UILabel alloc]init];
- _titleLb.textAlignment = NSTextAlignmentCenter;
- _titleLb.textColor = self.titleColor;
- _titleLb.font = self.titleFont;
- _titleLb.text = self.titleText;
- }
- return _titleLb;
- }
- - (UIButton *)cancelBtn {
- if(!_cancelBtn){
- _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _cancelBtn.backgroundColor = self.cancelBgColor;
- _cancelBtn.titleLabel.font = self.cancelFont;
- [_cancelBtn setTitleColor:self.cancelTitleColor forState:UIControlStateNormal];
- [_cancelBtn setTitle:self.cancelTitle forState:UIControlStateNormal];
- _cancelBtn.layer.cornerRadius = self.cancelRadius;
- _cancelBtn.layer.borderWidth = self.cancelBorderWidth;
- _cancelBtn.layer.borderColor = self.cancelBorderColor.CGColor;
- WS(weakSelf)
- [[[_cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
- if (weakSelf.buttonBlock) {
- weakSelf.buttonBlock(NO, @"",@"",@"",@"");
- }
- }];
- }
- return _cancelBtn;
- }
- - (UIButton *)confirmBtn {
- if(!_confirmBtn){
- _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _confirmBtn.backgroundColor = self.confirmBgColor;
- _confirmBtn.titleLabel.font = self.confirmFont;
- [_confirmBtn setTitleColor:self.confirmTitleColor forState:UIControlStateNormal];
- [_confirmBtn setTitle:self.confirmTitle forState:UIControlStateNormal];
- _confirmBtn.layer.cornerRadius = self.confirmRadius;
- _confirmBtn.layer.borderWidth = self.confirmBorderWidth;
- _confirmBtn.layer.borderColor = self.confirmBorderColor.CGColor;
- WS(weakSelf)
- [[[_confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
- if (weakSelf.buttonBlock) {
- weakSelf.buttonBlock(YES,
- self.currentSelectProvinceId,
- self.currentSelectProvince,
- self.currentSelectCityId,
- self.currentSelectCity);
- }
-
- }];
- }
- return _confirmBtn;
- }
- - (UIPickerView *)pickerView{
- if (!_pickerView) {
- _pickerView = [[UIPickerView alloc]init];
- _pickerView.delegate = self;
- _pickerView.dataSource = self;
- _pickerView.showsSelectionIndicator = YES;
- }
- return _pickerView;
- }
- - (NSMutableArray *)provinceArr {
- if (!_provinceArr) {
- _provinceArr = [NSMutableArray array];
- }
- return _provinceArr;
- }
- - (NSMutableArray *)cityArr {
- if (!_cityArr) {
- _cityArr = [NSMutableArray array];
- }
- return _cityArr;
- }
- @end
|