YMCityPickerPopupView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // YMCityPickerPopupView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/10.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMCityPickerPopupView.h"
  9. @interface YMCityPickerPopupView ()<UIPickerViewDelegate,UIPickerViewDataSource>
  10. /** 标题标签*/
  11. @property (nonatomic, strong) UILabel *titleLb;
  12. /** 取消按钮*/
  13. @property (nonatomic, strong) UIButton *cancelBtn;
  14. /** 确认按钮*/
  15. @property (nonatomic, strong) UIButton *confirmBtn;
  16. /** 选择器*/
  17. @property (nonatomic, strong) UIPickerView *pickerView;
  18. /** 全部数据*/
  19. @property (nonatomic, strong) NSArray <YMProvinceModel*>*allArr;
  20. /** 省份数据*/
  21. @property (nonatomic, strong) NSMutableArray *provinceArr;
  22. /** 城市数据*/
  23. @property (nonatomic, strong) NSMutableArray *cityArr;
  24. /** 当前选中省份*/
  25. @property (nonatomic, strong) NSString *currentSelectProvince;
  26. /** 当前选中城市*/
  27. @property (nonatomic, strong) NSString *currentSelectCity;
  28. /** 当前选中省份Id*/
  29. @property (nonatomic, strong) NSString *currentSelectProvinceId;
  30. /** 当前选中城市Id*/
  31. @property (nonatomic, strong) NSString *currentSelectCityId;
  32. @end
  33. @implementation YMCityPickerPopupView
  34. - (void)ym_setupViews{
  35. self.frame = CGRectMake(0, 0, kFrameWidth, 250);
  36. self.backgroundColor = HexColorFromRGB(0xFFFFFF);
  37. self.titleText = @"请选择选项";
  38. self.titleColor = HexColorFromRGB(0x000000);
  39. self.titleFont = LCFont(13);
  40. self.cancelBgColor = [UIColor clearColor];
  41. self.confirmBgColor = [UIColor clearColor];
  42. self.cancelTitleColor = HexColorFromRGB(0x333333);
  43. self.confirmTitleColor = HexColorFromRGB(0x333333);
  44. self.cancelFont = LCFont(14);
  45. self.confirmFont = LCFont(14);
  46. self.cancelTitle = @"取消";
  47. self.confirmTitle = @"确定";
  48. self.cancelRadius = 10;
  49. self.confirmRadius = 10;
  50. self.cancelBorderColor = [UIColor clearColor];
  51. self.confirmBorderColor = [UIColor clearColor];
  52. self.cancelBorderWidth = 0;
  53. self.confirmBorderWidth = 0;
  54. [self getDataArray];
  55. [self addSubview:self.cancelBtn];
  56. [self addSubview:self.titleLb];
  57. [self addSubview:self.confirmBtn];
  58. [self addSubview:self.pickerView];
  59. [self setNeedsUpdateConstraints];
  60. [self updateConstraintsIfNeeded];
  61. }
  62. - (void)updateConstraints{
  63. [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.top.equalTo(self).offset(adapt(10));
  65. make.left.equalTo(self).offset(adapt(10));
  66. make.width.mas_equalTo(adapt(60));
  67. make.height.mas_equalTo(adapt(30));
  68. }];
  69. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.top.equalTo(self).offset(adapt(10));
  71. make.left.equalTo(self.cancelBtn.mas_right).offset(adapt(10));
  72. make.right.equalTo(self.confirmBtn.mas_left).offset(adapt(-10));
  73. make.height.mas_equalTo(adapt(30));
  74. }];
  75. [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.top.equalTo(self).offset(adapt(10));
  77. make.right.equalTo(self).offset(adapt(-10));
  78. make.width.mas_equalTo(adapt(60));
  79. make.height.mas_equalTo(adapt(30));
  80. }];
  81. [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
  82. make.top.equalTo(self.titleLb.mas_bottom);
  83. make.left.equalTo(self);
  84. make.right.equalTo(self);
  85. make.bottom.equalTo(self).offset(adapt(-15));
  86. }];
  87. [super updateConstraints];
  88. }
  89. - (void)getDataArray {
  90. [self.provinceArr removeAllObjects];
  91. [self.cityArr removeAllObjects];
  92. NSArray *provinceCityArr = [OCUserDefaults objectForKey:kPROVINCE_CITY_ARR];
  93. if (!OCArrayIsEmpty(provinceCityArr)) {
  94. self.allArr = [NSArray yy_modelArrayWithClass:[YMProvinceModel class] json:provinceCityArr];
  95. } else if (!OCArrayIsEmpty([YMGlobalUtils shared].provinceCityArr)) {
  96. self.allArr = [NSArray yy_modelArrayWithClass:[YMProvinceModel class] json:[YMGlobalUtils shared].provinceCityArr];
  97. } else {
  98. [ZCHUDHelper showWithStatus:@"正在获取省市级数据"];
  99. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  100. [[YMGlobalUtils shared] requestProvinceCity];
  101. [[YMGlobalUtils shared] requestCityCode];
  102. dispatch_async(dispatch_get_main_queue(), ^{
  103. [ZCHUDHelper dismiss];
  104. });
  105. });
  106. return;
  107. }
  108. [self calculationCityAreaArr];
  109. [self.pickerView selectRow:[self.provinceArr indexOfObject:self.currentSelectProvince] inComponent:0 animated:YES];
  110. [self.pickerView selectRow:[self.cityArr indexOfObject:self.currentSelectCity] inComponent:1 animated:YES];
  111. }
  112. /// 计算当前市区数组
  113. - (void)calculationCityAreaArr {
  114. [self.provinceArr removeAllObjects];
  115. [self.cityArr removeAllObjects];
  116. if (!self.currentSelectProvince) {
  117. self.currentSelectProvince = self.allArr[0].provinceName;
  118. }
  119. for (YMProvinceModel *province in self.allArr) {
  120. [self.provinceArr addObject:province.provinceName];
  121. if ([self.currentSelectProvince isEqualToString:province.provinceName]) {
  122. if (!self.currentSelectCity) {
  123. self.currentSelectCity = province.cityArr[0].cityName;
  124. }
  125. for (YMCityModel *city in province.cityArr) {
  126. [self.cityArr addObject:city.cityName];
  127. if ([city.cityName isEqualToString:self.currentSelectCity]) {
  128. }
  129. }
  130. }
  131. }
  132. [self.pickerView reloadAllComponents];
  133. }
  134. //返回UIPickerView中Component列的宽度
  135. //- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
  136. // return 50;
  137. //}
  138. //返回UIPickerView中Component列中每行的高度
  139. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
  140. return 50;
  141. }
  142. //该方法返回值决定该控件包含多少列
  143. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
  144. return 2;
  145. }
  146. //该方法的返回值决定该控件指定列包含多少个列表项
  147. - (NSInteger)pickerView:(UIPickerView *)pickerView
  148. numberOfRowsInComponent:(NSInteger)component{
  149. if (component == 0) {
  150. return self.provinceArr.count;
  151. }else {
  152. return self.cityArr.count;
  153. }
  154. }
  155. // UIPickerView中指定列和列表项上显示的标题
  156. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
  157. (NSInteger)row forComponent:(NSInteger)component{
  158. if (component == 0) {
  159. return self.provinceArr[row];
  160. }else{
  161. return self.cityArr[row];
  162. }
  163. }
  164. // 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
  165. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  166. if (component == 0) {
  167. self.currentSelectProvince = self.provinceArr[row];
  168. self.currentSelectProvinceId = [self.allArr objectAtIndex:[self.provinceArr indexOfObject:self.currentSelectProvince]].provinceId;
  169. self.currentSelectCity = nil;
  170. [self calculationCityAreaArr];
  171. [pickerView selectRow:[self.cityArr indexOfObject:self.currentSelectCity] inComponent:1 animated:YES];
  172. }else if (component == 1) {
  173. self.currentSelectCity = self.cityArr[row];
  174. self.currentSelectCityId = [[self.allArr objectAtIndex:[self.provinceArr indexOfObject:self.currentSelectProvince]].cityArr objectAtIndex:[self.cityArr indexOfObject:self.currentSelectCity]].cityId;
  175. [self calculationCityAreaArr];
  176. }
  177. }
  178. - (void)setTitleText:(NSString *)titleText{
  179. _titleText = titleText;
  180. self.titleLb.text = titleText;
  181. }
  182. - (void)setCancelBgColor:(UIColor *)cancelBgColor{
  183. _cancelBgColor = cancelBgColor;
  184. _cancelBtn.backgroundColor = cancelBgColor;
  185. }
  186. - (void)setConfirmBgColor:(UIColor *)confirmBgColor{
  187. _confirmBgColor = confirmBgColor;
  188. self.confirmBtn.backgroundColor = confirmBgColor;
  189. }
  190. - (void)setCancelTitleColor:(UIColor *)cancelTitleColor{
  191. _cancelTitleColor = cancelTitleColor;
  192. [self.cancelBtn setTitleColor:cancelTitleColor forState:UIControlStateNormal];
  193. }
  194. - (void)setConfirmTitleColor:(UIColor *)confirmTitleColor{
  195. _confirmTitleColor = confirmTitleColor;
  196. [self.confirmBtn setTitleColor:confirmTitleColor forState:UIControlStateNormal];
  197. }
  198. - (void)setCancelFont:(UIFont *)cancelFont{
  199. _cancelFont = cancelFont;
  200. self.cancelBtn.titleLabel.font = cancelFont;
  201. }
  202. - (void)setConfirmFont:(UIFont *)confirmFont{
  203. _confirmFont = confirmFont;
  204. self.confirmBtn.titleLabel.font = confirmFont;
  205. }
  206. - (void)setCancelTitle:(NSString *)cancelTitle{
  207. _cancelTitle = cancelTitle;
  208. [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
  209. }
  210. - (void)setConfirmTitle:(NSString *)confirmTitle{
  211. _confirmTitle = confirmTitle;
  212. [self.confirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
  213. }
  214. - (void)setCancelRadius:(CGFloat)cancelRadius{
  215. _cancelRadius = cancelRadius;
  216. self.cancelBtn.layer.cornerRadius = cancelRadius;
  217. }
  218. - (void)setConfirmRadius:(CGFloat)confirmRadius{
  219. _confirmRadius = confirmRadius;
  220. self.confirmBtn.layer.cornerRadius = confirmRadius;
  221. }
  222. - (void)setCancelBorderColor:(UIColor *)cancelBorderColor{
  223. _cancelBorderColor = cancelBorderColor;
  224. self.cancelBtn.layer.borderColor = cancelBorderColor.CGColor;
  225. }
  226. - (void)setConfirmBorderColor:(UIColor *)confirmBorderColor{
  227. _confirmBorderColor = confirmBorderColor;
  228. self.confirmBtn.layer.borderColor = confirmBorderColor.CGColor;
  229. }
  230. - (void)setCancelBorderWidth:(CGFloat)cancelBorderWidth{
  231. _cancelBorderWidth = cancelBorderWidth;
  232. self.cancelBtn.layer.borderWidth = cancelBorderWidth;
  233. }
  234. - (void)setConfirmBorderWidth:(CGFloat)confirmBorderWidth{
  235. _confirmBorderWidth = confirmBorderWidth;
  236. self.confirmBtn.layer.borderWidth = confirmBorderWidth;
  237. }
  238. - (UILabel *)titleLb{
  239. if (!_titleLb) {
  240. _titleLb = [[UILabel alloc]init];
  241. _titleLb.textAlignment = NSTextAlignmentCenter;
  242. _titleLb.textColor = self.titleColor;
  243. _titleLb.font = self.titleFont;
  244. _titleLb.text = self.titleText;
  245. }
  246. return _titleLb;
  247. }
  248. - (UIButton *)cancelBtn {
  249. if(!_cancelBtn){
  250. _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  251. _cancelBtn.backgroundColor = self.cancelBgColor;
  252. _cancelBtn.titleLabel.font = self.cancelFont;
  253. [_cancelBtn setTitleColor:self.cancelTitleColor forState:UIControlStateNormal];
  254. [_cancelBtn setTitle:self.cancelTitle forState:UIControlStateNormal];
  255. _cancelBtn.layer.cornerRadius = self.cancelRadius;
  256. _cancelBtn.layer.borderWidth = self.cancelBorderWidth;
  257. _cancelBtn.layer.borderColor = self.cancelBorderColor.CGColor;
  258. WS(weakSelf)
  259. [[[_cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  260. if (weakSelf.buttonBlock) {
  261. weakSelf.buttonBlock(NO, @"",@"",@"",@"");
  262. }
  263. }];
  264. }
  265. return _cancelBtn;
  266. }
  267. - (UIButton *)confirmBtn {
  268. if(!_confirmBtn){
  269. _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  270. _confirmBtn.backgroundColor = self.confirmBgColor;
  271. _confirmBtn.titleLabel.font = self.confirmFont;
  272. [_confirmBtn setTitleColor:self.confirmTitleColor forState:UIControlStateNormal];
  273. [_confirmBtn setTitle:self.confirmTitle forState:UIControlStateNormal];
  274. _confirmBtn.layer.cornerRadius = self.confirmRadius;
  275. _confirmBtn.layer.borderWidth = self.confirmBorderWidth;
  276. _confirmBtn.layer.borderColor = self.confirmBorderColor.CGColor;
  277. WS(weakSelf)
  278. [[[_confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  279. if (weakSelf.buttonBlock) {
  280. weakSelf.buttonBlock(YES,
  281. self.currentSelectProvinceId,
  282. self.currentSelectProvince,
  283. self.currentSelectCityId,
  284. self.currentSelectCity);
  285. }
  286. }];
  287. }
  288. return _confirmBtn;
  289. }
  290. - (UIPickerView *)pickerView{
  291. if (!_pickerView) {
  292. _pickerView = [[UIPickerView alloc]init];
  293. _pickerView.delegate = self;
  294. _pickerView.dataSource = self;
  295. _pickerView.showsSelectionIndicator = YES;
  296. }
  297. return _pickerView;
  298. }
  299. - (NSMutableArray *)provinceArr {
  300. if (!_provinceArr) {
  301. _provinceArr = [NSMutableArray array];
  302. }
  303. return _provinceArr;
  304. }
  305. - (NSMutableArray *)cityArr {
  306. if (!_cityArr) {
  307. _cityArr = [NSMutableArray array];
  308. }
  309. return _cityArr;
  310. }
  311. @end