123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- //
- // YLAreaPickerView.m
- // LiveTV
- //
- // Created by 翟玉磊 on 2019/4/22.
- // Copyright © 2019 翟玉磊. All rights reserved.
- //
- #import "YLAreaPickerView.h"
- #import "ProvincOrCityOrAreaModel.h"
- static const NSInteger KProvinceComponent = 0;
- static const NSInteger KCityComponent = 1;
- static const NSInteger KDistrictComponent = 2;
- #define KFont14 [UIFont systemFontOfSize:14]
- @interface YLAreaPickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>
- {
- NSInteger _selectRow0;//记录第0个轮子的选择行
-
- NSInteger _selectRow1;
-
- NSInteger _selectRow2;
-
- NSString *_areaString;//最后要传回的详细地址拼接字符串
-
- NSDictionary *_infoDic;
-
- ProvincOrCityOrAreaModel *_proModel;//记录下选中省的数据
-
- ProvincOrCityOrAreaModel *_cityModel;
-
- ProvincOrCityOrAreaModel *_areaModel;
- }
- @property (nonatomic, readwrite, strong) UIButton *bgView;
- @property (nonatomic, readwrite, strong) UIView *infoView;
- @property (nonatomic, readwrite, strong) UIView *titleView;
- @property (nonatomic, readwrite, strong) UILabel *titleLabel;
- @property (nonatomic, readwrite, strong) UIButton *cancelButton;
- @property (nonatomic, readwrite, strong) UIButton *sureButton;
- @property (strong, nonatomic) UIPickerView *pickerView;
- @property (strong, nonatomic) ProvincOrCityOrAreaModel *provBridge;
- @property (strong, nonatomic) ProvincOrCityOrAreaModel *cityBridge;
- @property (strong, nonatomic) ProvincOrCityOrAreaModel *areaBridge;
- @property (strong, nonatomic) NSMutableArray *provDataArr;//省数组
- @property (strong, nonatomic) NSMutableArray *cityDataArr;//市数组
- @property (strong, nonatomic) NSMutableArray * areaDataArr;//区数组
- @property (nonatomic, readwrite, copy) NSString *selectProvId;
- @property (nonatomic, readwrite, copy) NSString *selectCityId;
- @property (nonatomic, readwrite, copy) NSString *selectAreaId;
- @end
- @implementation YLAreaPickerView
- #pragma mark - Public
- + (instancetype)areaPickerWithTitle:(NSString *)title target:(id<YLAreaPickerViewDelegate>)target {
- YLAreaPickerView *picker = [[YLAreaPickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
- picker.titleLabel.text = title;
- picker.delegate = target;
- [[AppDelegate sharedDelegate].window addSubview:picker];
- return picker;
- }
- - (void)show {
- self.hidden = NO;
- [UIView animateWithDuration:.3 animations:^{
- self.infoView.frame = CGRectMake(0, self.f_heigh - 264.0f, self.f_width, 264.0f);
- } completion:^(BOOL finished) {
- }];
- }
- - (void)dismiss {
- [UIView animateWithDuration:.3 animations:^{
- self.infoView.frame = CGRectMake(0, self.f_heigh, self.f_width, 264.0f);
- } completion:^(BOOL finished) {
- self.hidden = YES;
- [self removeFromSuperview];
- }];
- }
- - (void)setupCityWithAreaPath:(NSString *)areaPath {
- NSArray *areaIds = [areaPath componentsSeparatedByString:@","];
- if (areaIds.count == 0) {
- [self downloadProvWithAreaId:@""];
- return;
- }
- for (NSInteger i = 0; i < areaIds.count; i++) {
- NSString *areaId = [areaIds objectAtIndex:i];
- if (StringIsNotEmpty(areaId)) {
- switch (i) {
- case 0:
- self.selectProvId = areaId;
- break;
- case 1:
- self.selectCityId = areaId;
- break;
- case 2:
- self.selectAreaId = areaId;
- break;
- default:
- break;
- }
- }
- }
- [self downloadProvWithAreaId:self.selectProvId];
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self _setupView];
- }
- return self;
- }
- - (void)_setupView {
- _selectRow0 = 0;
- _selectRow1 = 0;
- _selectRow2 = 0;
- self.provDataArr = [[NSMutableArray alloc]init];
- self.cityDataArr = [[NSMutableArray alloc]init];
- self.areaDataArr = [[NSMutableArray alloc]init];
- [self addSubview:self.bgView];
- [self addSubview:self.infoView];
- [self.infoView addSubview:self.titleView];
- [self.infoView addSubview:self.pickerView];
- [self.titleView addSubview:self.cancelButton];
- [self.titleView addSubview:self.titleLabel];
- [self.titleView addSubview:self.sureButton];
- self.bgView.frame = self.bounds;
- self.infoView.frame = CGRectMake(0, self.f_heigh, self.f_width, 264.0f);
- [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.right.equalTo(self.infoView);
- make.height.equalTo(@44.0f);
- }];
- [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.bottom.equalTo(self.infoView);
- make.top.equalTo(self.titleView.mas_bottom);
- }];
- [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.titleView).offset(10.0f);
- make.width.height.equalTo(@44.0f);
- make.centerY.equalTo(self.titleView);
- }];
- [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.titleView).offset(-10.0f);
- make.width.height.equalTo(@44.0f);
- make.centerY.equalTo(self.titleView);
- }];
- [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.cancelButton).offset(10.0f);
- make.right.equalTo(self.sureButton).offset(-10.0f);
- make.centerY.equalTo(self.titleView);
- }];
- [self.bgView addTarget:self action:@selector(selectAddressClose) forControlEvents:UIControlEventTouchUpInside];
- [self.cancelButton addTarget:self action:@selector(selectAddressClose) forControlEvents:UIControlEventTouchUpInside];
- [self.sureButton addTarget:self action:@selector(selectAddressComplete) forControlEvents:UIControlEventTouchUpInside];
- self.hidden = YES;
- }
- #pragma mark - http methods
- - (void)downloadProvWithAreaId:(NSString *)areaId {
- [[XYConfigAPIManager new] getAreaListWithParentId:nil successHandler:^(ZYLResponseModel *responseModel) {
- [self.provDataArr removeAllObjects];
- if (responseModel.codeState) {
- NSMutableArray *temp = [NSMutableArray array];
- for (NSDictionary *dict in responseModel.result[@"data"][@"list"]) {
- ProvincOrCityOrAreaModel *model = [ProvincOrCityOrAreaModel new];
- [model yy_modelSetWithDictionary:dict];
- [temp addObject:model];
- }
- [self.provDataArr addObjectsFromArray:temp];
- if (StringIsEmpty(areaId)) {
- [self pickerView:self.pickerView didSelectRow:0 inComponent:0 ];
- [self.pickerView reloadAllComponents];
- }else {
- for (NSInteger i = 0; i < self.provDataArr.count; i++) {
- ProvincOrCityOrAreaModel *model = self.provDataArr[i];
- if ([model.areaId isEqualToString:areaId]) {
- self->_proModel = model;
- [self.pickerView reloadAllComponents];
- [self.pickerView selectRow:i inComponent:KProvinceComponent animated:YES];
- [self downloadCityWithId:self.selectProvId selectedAreaId:self.selectCityId];
- break;
- }
- }
- }
- }
- } failureHandler:^(NSError *error) {
- }];
- }
- - (void)downloadCityWithId:(NSString *)provId selectedAreaId:(NSString *)selectedAreaId {
- [[XYConfigAPIManager new] getAreaListWithParentId:provId successHandler:^(ZYLResponseModel *responseModel) {
- [self.cityDataArr removeAllObjects];
- if (responseModel.codeState) {
- NSMutableArray *temp = [NSMutableArray array];
- for (NSDictionary *dict in responseModel.result[@"data"][@"list"]) {
- ProvincOrCityOrAreaModel *model = [ProvincOrCityOrAreaModel new];
- [model yy_modelSetWithDictionary:dict];
- [temp addObject:model];
- }
- [self.cityDataArr addObjectsFromArray:temp];
- if (StringIsEmpty(selectedAreaId)) {
- [self.pickerView reloadComponent:1];
- [self.pickerView selectRow:0 inComponent:1 animated:YES];
- [self pickerView:self.pickerView didSelectRow:0 inComponent:1 ];
- self->_cityModel = self.cityDataArr[self->_selectRow1];
- [self downloadAreaWithId:self->_cityModel.areaId selectedAreaId:@""];
- }else {
- for (NSInteger i = 0; i < self.cityDataArr.count; i++) {
- ProvincOrCityOrAreaModel *model = self.cityDataArr[i];
- if ([model.areaId isEqualToString:selectedAreaId]) {
- self->_cityModel = model;
- self->_infoDic = @{@"proID":self->_proModel.areaId,
- @"cityId":self->_cityModel.areaId,
- @"areaName":self->_cityModel.areaName,
- };
- self->_areaString = [NSString stringWithFormat:@"%@%@",self->_proModel.areaName,self->_cityModel.areaName];
- [self.pickerView reloadAllComponents];
- [self.pickerView selectRow:i inComponent:KCityComponent animated:YES];
- [self downloadAreaWithId:self.selectCityId selectedAreaId:self.selectAreaId];
- break;
- }
- }
- }
- }
- } failureHandler:^(NSError *error) {
- }];
- }
- - (void)downloadAreaWithId:(NSString *)cityId selectedAreaId:(NSString *)selectedAreaId {
- [[XYConfigAPIManager new] getAreaListWithParentId:cityId successHandler:^(ZYLResponseModel *responseModel) {
- [self.areaDataArr removeAllObjects];
- if (responseModel.codeState) {
- NSMutableArray *temp = [NSMutableArray array];
- for (NSDictionary *dict in responseModel.result[@"data"][@"list"]) {
- ProvincOrCityOrAreaModel *model = [ProvincOrCityOrAreaModel new];
- [model yy_modelSetWithDictionary:dict];
- [temp addObject:model];
- }
- [self.areaDataArr addObjectsFromArray:temp];
- if (StringIsEmpty(selectedAreaId)) {
- [self.pickerView reloadComponent:2];
- [self.pickerView selectRow:0 inComponent:2 animated:YES];
- [self pickerView:self.pickerView didSelectRow:0 inComponent:2];
- }else {
- for (NSInteger i = 0; i < self.areaDataArr.count; i++) {
- ProvincOrCityOrAreaModel *model = self.areaDataArr[i];
- if ([model.areaId isEqualToString:selectedAreaId]) {
- self->_areaModel = model;
- self->_infoDic = @{@"proID":self->_proModel.areaId,
- @"cityId":self->_cityModel.areaId,
- @"areaId":self->_areaModel.areaId,
- @"areaName":self->_areaModel.areaName,
- };
- self->_areaString = [NSString stringWithFormat:@"%@%@%@",self->_proModel.areaName,self->_cityModel.areaName,self->_areaModel.areaName];
- [self.pickerView reloadAllComponents];
- [self.pickerView selectRow:i inComponent:KDistrictComponent animated:YES];
- break;
- }
- }
- }
- }
- } failureHandler:^(NSError *error) {
- }];
- }
- #pragma mark - events response
- - (void)selectAddressComplete {
- if (self.delegate && [self.delegate respondsToSelector:@selector(addressPickerViewDidSelected:withDic:)]) {
- [self.delegate addressPickerViewDidSelected:_areaString withDic:_infoDic];
- }
- [self dismiss];
- }
- - (void)selectAddressClose {
- if (self.delegate && [self.delegate respondsToSelector:@selector(addressPickerViewDidClose)]) {
- [self.delegate addressPickerViewDidClose];
- }
- [self dismiss];
- }
- #pragma mark - UIPickerViewDataSource
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
- return 3;
- }
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
- if (component==0) {
- return self.provDataArr.count;
- }else if(component==1){
- return self.cityDataArr.count;
- }else{
- return self.areaDataArr.count;
- }
- }
- - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
- return 40;
- }
- - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
- NSDictionary * attrDic = @{NSForegroundColorAttributeName:ColorFromHexString(@"#453738"),NSFontAttributeName:Font_B(16)};
- ProvincOrCityOrAreaModel *provice = [[ProvincOrCityOrAreaModel alloc]init];
- ProvincOrCityOrAreaModel *city = [[ProvincOrCityOrAreaModel alloc]init];
- ProvincOrCityOrAreaModel *area = [[ProvincOrCityOrAreaModel alloc]init];
- NSAttributedString * attrString;
- if (component==0) {
- provice = (ProvincOrCityOrAreaModel *)self.provDataArr[row];
- attrString = [[NSAttributedString alloc] initWithString:provice.areaName attributes:attrDic];
- }else if(component==1){
- city = (ProvincOrCityOrAreaModel *)self.cityDataArr[row];
- attrString = [[NSAttributedString alloc] initWithString:city.areaName attributes:attrDic];
- }else{
- area = (ProvincOrCityOrAreaModel *)self.areaDataArr[row];
- attrString = [[NSAttributedString alloc] initWithString:area.areaName attributes:attrDic];
- }
- return attrString;
- }
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
- if (component==0) {
- _selectRow0 = [pickerView selectedRowInComponent:0];
- _selectRow1 = 0;
- _selectRow2 = 0;
- _proModel = self.provDataArr[_selectRow0];
- [self downloadCityWithId:_proModel.areaId selectedAreaId:@""];
- }else if(component==1){
- _selectRow1 = [pickerView selectedRowInComponent:1];
- _selectRow2 = 0;
- _cityModel = self.cityDataArr[_selectRow1];
- [self downloadAreaWithId:_cityModel.areaId selectedAreaId:@""];
- }else{
- _selectRow2 = [pickerView selectedRowInComponent:2];
- if (self.areaDataArr&&self.areaDataArr.count>0) {
- _areaModel = self.areaDataArr[_selectRow2];
- }else{
- _areaModel = nil;
- }
- }
- if (_areaModel==nil) {
- _areaString = [NSString stringWithFormat:@"%@%@",_proModel.areaName,_cityModel.areaName];
- if (_proModel && _cityModel) {
- _infoDic = @{@"proID":_proModel.areaId,
- @"cityId":_cityModel.areaId,
- @"areaName":_cityModel.areaName,
- };
- }
- }else{
- _areaString = [NSString stringWithFormat:@"%@%@%@",_proModel.areaName,_cityModel.areaName,_areaModel.areaName];
- if (_proModel && _cityModel && _areaModel) {
- _infoDic = @{@"proID":_proModel.areaId,
- @"cityId":_cityModel.areaId,
- @"areaId":_areaModel.areaId,
- @"areaName":_areaModel.areaName,
- };
- }
- }
- [pickerView reloadAllComponents];
- }
- - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
- for(UIView *singleLine in pickerView.subviews)
- {
- if (singleLine.frame.size.height < 1)
- {
- singleLine.backgroundColor = Color_line;
- singleLine.frame = CGRectMake(20.0f, singleLine.f_y, pickerView.f_width - 40.0f, singleLine.f_heigh);
- }
- }
- CGFloat subViewW = (pickerView.frame.size.width - 40.0f) / 3;
- CGFloat subViewH = 44.0f;
- UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, subViewW, subViewH)];
- subView.backgroundColor = Color_White;
- UILabel *myView = [[UILabel alloc] initWithFrame:subView.frame];;
- myView.backgroundColor = [UIColor clearColor];
- myView.textAlignment = NSTextAlignmentCenter;
- if (component == KProvinceComponent) {
- self.provBridge = [_provDataArr objectAtIndex:row];
- myView.text = self.provBridge.areaName;
- if (_selectRow0 == row) {
- myView.textColor = ColorFromHexString(@"#453738");
- myView.font = Font_B(18);
- }else {
- myView.textColor = ColorFromHexString(@"#453738");
- myView.font = Font_B(16);
- }
- }
- else if (component == KCityComponent) {
- self.cityBridge = [_cityDataArr objectAtIndex:row];
- myView.text = self.cityBridge.areaName;
- if (_selectRow1 == row) {
- myView.textColor = ColorFromHexString(@"#453738");
- myView.font = Font_B(18);
- }else {
- myView.textColor = ColorFromHexString(@"#453738");
- myView.font = Font_B(16);
- }
- }
- else {
- self.areaBridge = [_areaDataArr objectAtIndex:row];
- myView.text = self.areaBridge.areaName;
- if (_selectRow2 == row) {
- myView.textColor = ColorFromHexString(@"#453738");
- myView.font = Font_B(18);
- }else {
- myView.textColor = ColorFromHexString(@"#453738");
- myView.font = Font_B(16);
- }
- }
- [subView addSubview:myView];
- return subView;
- }
- #pragma mark - getters and setters
- - (UIButton *)bgView {
- if (_bgView == nil) {
- _bgView = [UIButton buttonWithType:UIButtonTypeCustom];
- _bgView.backgroundColor = ColorFromHexString(@"#453738");
- _bgView.alpha = 0.4f;
- }
- return _bgView;
- }
- - (UIView *)infoView {
- if (_infoView == nil) {
- _infoView = [UIView new];
- _infoView.backgroundColor = Color_White;
- }
- return _infoView;
- }
- - (UIView *)titleView {
- if (_titleView == nil) {
- _titleView = [[UIView alloc] init];
- _titleView.backgroundColor = ColorFromHexString(@"#FAFAFA");
- }
- return _titleView;
- }
- - (UIButton *)cancelButton {
- if (_cancelButton == nil) {
- _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_cancelButton setTitle:kLocalizedString(@"取消") forState:UIControlStateNormal];
- [_cancelButton setTitleColor:ColorFromHexString(@"#999999") forState:UIControlStateNormal];
- _cancelButton.titleLabel.font = Font(16);
- }
- return _cancelButton;
- }
- - (UIButton *)sureButton {
- if (_sureButton == nil) {
- _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_sureButton setTitle:kLocalizedString(@"确认") forState:UIControlStateNormal];
- [_sureButton setTitleColor:Color_Blue forState:UIControlStateNormal];
- _sureButton.titleLabel.font = Font_B(16);
- }
- return _sureButton;
- }
- - (UILabel *)titleLabel {
- if (_titleLabel == nil) {
- _titleLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font(16)];
- _titleLabel.text = kLocalizedString(@"地区选择");
- _titleLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _titleLabel;
- }
- - (UIPickerView *)pickerView {
- if (_pickerView == nil) {
- _pickerView = [[UIPickerView alloc] init];
- _pickerView.delegate = self;
- _pickerView.dataSource = self;
- }
- return _pickerView;
- }
- @end
|