ZKAddressPickView
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// // ZKAddressPickView.h // GoodsManager // // Created by HELLO WORLD on 2019/9/8. // Copyright © 2019年 HELLO WORLD. All rights reserved. // #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @class ZKAddressToolBar,ZKAddressPicker; typedef void(^ZKAddressPickViewPickBlock)(NSDictionary *dic); @interface ZKAddressPickView : UIView /// Default is 0.3. @property (nonatomic,assign) CGFloat grayViewAlpha; /// Default is 'NO'. @property (nonatomic,assign) BOOL hideWhenTapGrayView; /// Default is 3, two value to set: 2 or 3, @property (nonatomic,assign) NSInteger columns; /// @property (nonatomic,copy)ZKAddressPickViewPickBlock pickBlock; /// @property (nonatomic,strong,readonly) ZKAddressToolBar *toolBar; /// @property (nonatomic,strong,readonly) ZKAddressPicker *picker; @property (nonatomic, strong) NSArray *dataArray; - (void)showInView:(UIView *)view; - (void)hide; @end NS_ASSUME_NONNULL_END
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// // ZKAddressPickView.m // GoodsManager // // Created by HELLO WORLD on 2019/9/8. // Copyright © 2019年 HELLO WORLD. All rights reserved. // #import "ZKAddressPickView.h" #import "ZKAddressToolBar.h" #import "ZKAddressPicker.h" @interface ZKAddressPickView()<ZKAddressToolBarDelegate,ZKAddressPickerDelegate> @property (nonatomic, strong) UIView *grayView; @property (nonatomic, strong) UIButton *dissmissButton; @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) ZKAddressToolBar *toolBar; @property (nonatomic, strong) ZKAddressPicker *picker; @property (nonatomic, strong) NSMutableDictionary *resultDic; @end @implementation ZKAddressPickView - (instancetype)initWithFrame:(CGRect)frame { frame = [UIScreen mainScreen].bounds; self = [super initWithFrame:frame]; if (self) { _grayViewAlpha = 0.3; _columns = 3; _resultDic = @{}.mutableCopy; [self xjh_setupViews]; } return self; } - (void)xjh_setupViews { [self addSubview:self.grayView]; [self addSubview:self.contentView]; [_contentView addSubview:self.toolBar]; [_contentView addSubview:self.picker]; } - (UIView *)grayView{ if (!_grayView) { _grayView = [[UIView alloc] init]; _grayView.frame = self.bounds; _grayView.backgroundColor = [UIColor colorWithWhite:0 alpha:0]; } return _grayView; } - (UIButton *)dissmissButton{ if (!_dissmissButton) { _dissmissButton = [[UIButton alloc] init]; _dissmissButton.frame = self.bounds; [_dissmissButton addTarget:self action:@selector(hide) forControlEvents:1<<6]; [_grayView addSubview:_dissmissButton]; } return _dissmissButton; } - (UIView *)contentView{ if (!_contentView) { _contentView = [[UIView alloc] init]; _contentView.frame = CGRectMake(0, CGRectGetMaxY(self.bounds), CGRectGetWidth(self.bounds), 300); _contentView.backgroundColor = [UIColor whiteColor]; } return _contentView; } - (ZKAddressToolBar *)toolBar{ if (!_toolBar) { _toolBar = [[ZKAddressToolBar alloc] init]; _toolBar.delegate = self; } return _toolBar; } - (ZKAddressPicker *)picker{ if (!_picker) { _picker = [[ZKAddressPicker alloc] init]; _picker.frame = CGRectMake(0, 50, CGRectGetWidth(self.bounds), 200); _picker.delegate = self; } return _picker; } -(void)setDataArray:(NSArray *)dataArray{ _dataArray = dataArray; self.picker.dataArray = _dataArray; [self.picker loadData]; } #pragma mark - public - (void)setHideWhenTapGrayView:(BOOL)hideWhenTapGrayView{ _hideWhenTapGrayView = hideWhenTapGrayView; if (hideWhenTapGrayView) { self.dissmissButton.hidden = NO; }else{ self.dissmissButton.hidden = YES; } } - (void)setColumns:(NSInteger)columns{ if (_columns == 2 || _columns == 3) { _columns = columns; _picker.columns = columns; } } - (void)showInView:(UIView *)view{ if (!view) { return; } [view addSubview:self]; [UIView animateWithDuration:0.25 animations:^{ [self viewAnimation:_grayViewAlpha height:-CGRectGetHeight(_contentView.frame)]; }]; } - (void)hide{ [UIView animateWithDuration:0.25 animations:^{ [self viewAnimation:0 height:CGRectGetHeight(_contentView.frame)]; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } #pragma mark - private - (void)viewAnimation:(CGFloat)alpha height:(CGFloat)height { _grayView.backgroundColor = [UIColor colorWithWhite:0 alpha:alpha]; CGRect frame = _contentView.frame; frame.origin.y += height; _contentView.frame = frame; } #pragma mark - ZKAddressToolBarDelegate - (void)toolBarDidClickButton:(NSInteger)leftOrRight{ // left - 0, right - 1 if (leftOrRight == 1) { if (_pickBlock) { _pickBlock(_resultDic); } } [self hide]; } #pragma mark - ZKAddressPickerDelegate - (void)addressPickerDidSelectedRow:(NSInteger)row1 row2:(NSInteger)row2 row3:(NSInteger)row3{ //NSLog(@"row1:%@, row2:%@, row3:%@",@(row1),@(row2),@(row3)); NSString *province = nil; NSString *provinceCode = nil; NSString *city = nil; NSString *cityCode = nil; NSString *town = nil; NSString *townCode = nil; NSMutableString *mstr = @"".mutableCopy; NSDictionary *dic1 = _dataArray[row1]; province = dic1[@"name"]; provinceCode = dic1[@"code"]; [mstr appendString:province]; NSArray *array2 = [dic1 valueForKeyPath:@"children"]; if (row2 < array2.count) { NSDictionary *dic2 = array2[row2]; city = dic2[@"name"]; cityCode = dic2[@"code"]; [mstr appendString:city]; if (_columns == 3) { NSArray *array3 = [dic2 valueForKeyPath:@"grandChildren"]; if (row3 < array3.count) { NSDictionary *dic3 = array3[row3]; town = dic3[@"name"]; townCode = dic3[@"code"]; [mstr appendString:town]; } } } _toolBar.titleLable.text = mstr; [_resultDic setValue:province forKey:@"province"]; [_resultDic setValue:provinceCode forKey:@"provinceCode"]; [_resultDic setValue:city forKey:@"city"]; [_resultDic setValue:cityCode forKey:@"cityCode"]; [_resultDic setValue:town forKey:@"town"]; [_resultDic setValue:townCode forKey:@"townCode"]; //NSLog(@"%@",_resultDic); } @end
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// // ZKAddressPicker.h // GoodsManager // // Created by HELLO WORLD on 2019/9/8. // Copyright © 2019年 HELLO WORLD. All rights reserved. // #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @protocol ZKAddressPickerDelegate <NSObject> - (void)addressPickerDidSelectedRow:(NSInteger)row1 row2:(NSInteger)row2 row3:(NSInteger)row3; @end @interface ZKAddressPicker : UIView /// Default is 3, two value to set: 2 or 3, @property (nonatomic, assign) NSInteger columns; /// @property (nonatomic, strong) NSArray *dataArray; @property (nonatomic, weak) id <ZKAddressPickerDelegate> delegate; - (void)loadData; @end NS_ASSUME_NONNULL_END
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// // ZKAddressPicker.m // GoodsManager // // Created by HELLO WORLD on 2019/9/8. // Copyright © 2019年 HELLO WORLD. All rights reserved. // #import "ZKAddressPicker.h" @interface ZKAddressPicker()<UIPickerViewDelegate,UIPickerViewDataSource> @property (nonatomic, strong) UIPickerView *pickView; @property (nonatomic, strong) NSArray *provinceArray; @property (nonatomic, strong) NSArray *cityArray; @property (nonatomic, strong) NSArray *townArray; @property (nonatomic, assign) NSInteger selectRow1; @property (nonatomic, assign) NSInteger selectRow2; @property (nonatomic, assign) NSInteger selectRow3; @end @implementation ZKAddressPicker - (instancetype)initWithFrame:(CGRect)frame { frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 200); self = [super initWithFrame:frame]; if (self) { _columns = 3; [self xjh_setupViews]; } return self; } - (void)xjh_setupViews { self.backgroundColor = [UIColor whiteColor]; [self addSubview:self.pickView]; } - (UIPickerView *)pickView{ if (!_pickView) { _pickView = [[UIPickerView alloc] init]; _pickView.frame = self.bounds; _pickView.delegate = self; } return _pickView; } - (void)setColumns:(NSInteger)columns{ if (_columns == 2 || _columns == 3) { _columns = columns; } } - (void)loadData{ _provinceArray = [_dataArray valueForKey:@"name"]; _cityArray = [_dataArray[_selectRow1] valueForKeyPath:@"children.name"]; if (_columns == 3) { NSDictionary *dic = [_dataArray[_selectRow1] valueForKey:@"children"][_selectRow2]; _townArray = [dic valueForKeyPath:@"grandChildren.name"]; } [_pickView reloadAllComponents]; [_pickView selectRow:_selectRow1 inComponent:0 animated:YES]; [_pickView selectRow:_selectRow2 inComponent:1 animated:YES]; if (_columns == 3) { [_pickView selectRow:_selectRow3 inComponent:2 animated:YES]; } if (_delegate && [_delegate respondsToSelector:@selector(addressPickerDidSelectedRow:row2:row3:)]) { [_delegate addressPickerDidSelectedRow:_selectRow1 row2:_selectRow2 row3:_selectRow3]; } } #pragma mark - UIPickerViewDataSource /// 几列 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return _columns; } /// 一列几行 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (_dataArray.count == 0) { return 0; } if (component == 0) { return _provinceArray.count; }else if (component == 1) { return _cityArray.count; }else if (component == 2) { return _townArray.count; } return 0; } #pragma mark - UIPickerViewDelegate - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == 0) { _selectRow1 = row; _selectRow2 = 0; _selectRow3 = 0; _cityArray = [_dataArray[row] valueForKeyPath:@"children.name"]; [pickerView reloadComponent:1]; [pickerView selectRow:0 inComponent:1 animated:YES]; if (_columns == 3) { NSDictionary *dic = [_dataArray[row] valueForKey:@"children"][0]; _townArray = [dic valueForKeyPath:@"grandChildren.name"]; [pickerView reloadComponent:2]; [pickerView selectRow:0 inComponent:2 animated:YES]; } }else if (component == 1) { _selectRow2 = row; _selectRow3 = 0; if (_columns == 3) { NSDictionary *dic = [_dataArray[_selectRow1] valueForKey:@"children"][row]; _townArray = [dic valueForKeyPath:@"grandChildren.name"]; [pickerView reloadComponent:2]; [pickerView selectRow:0 inComponent:2 animated:YES]; } }else if (component == 2) { _selectRow3 = row; } if (_delegate && [_delegate respondsToSelector:@selector(addressPickerDidSelectedRow:row2:row3:)]) { [_delegate addressPickerDidSelectedRow:_selectRow1 row2:_selectRow2 row3:_selectRow3]; } } #if 0 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == 0) { return _provinceArray[row]; }else if (component == 1) { return _cityArray[row]; }else if (component == 2) { return _townArray[row]; } return @""; } #endif - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ return CGRectGetWidth([UIScreen mainScreen].bounds)/3; } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return 50; } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{ UILabel *label = (UILabel *)view; if (!label) { label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:14]; label.textAlignment = 1; //label.textColor = [UIColor whiteColor]; } if (component == 0) { if (row < _provinceArray.count) { label.text = _provinceArray[row]; } }else if (component == 1) { if (row < _cityArray.count) { label.text = _cityArray[row]; } }else if (component == 2) { if (row < _townArray.count) { label.text = _townArray[row]; } } //[label sizeToFit]; return label; } @end
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// // ZKAddressToolBar.h // GoodsManager // // Created by HELLO WORLD on 2019/9/8. // Copyright © 2019年 HELLO WORLD. All rights reserved. // #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @protocol ZKAddressToolBarDelegate<NSObject> /// left - 0, right - 1 - (void)toolBarDidClickButton:(NSInteger)leftOrRight; @end @interface ZKAddressToolBar : UIView @property (nonatomic, strong, readonly) UIButton *leftButton; @property (nonatomic, strong, readonly) UILabel *titleLable; @property (nonatomic, strong, readonly) UIButton *rightButton; @property (nonatomic, weak) id<ZKAddressToolBarDelegate> delegate; @end NS_ASSUME_NONNULL_END
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// // ZKAddressToolBar.m // GoodsManager // // Created by HELLO WORLD on 2019/9/8. // Copyright © 2019年 HELLO WORLD. All rights reserved. // #import "ZKAddressToolBar.h" #define HAddressToolBar_Button_Width 70 @interface ZKAddressToolBar() @property (nonatomic, strong) UIButton *leftButton; @property (nonatomic, strong) UILabel *titleLable; @property (nonatomic, strong) UIButton *rightButton; @end @implementation ZKAddressToolBar - (instancetype)initWithFrame:(CGRect)frame { frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 50); self = [super initWithFrame:frame]; if (self) { [self xjh_setupViews]; } return self; } - (void)xjh_setupViews { self.backgroundColor = [UIColor whiteColor]; [self addSubview:self.leftButton]; [self addSubview:self.titleLable]; [self addSubview:self.rightButton]; } - (UIButton *)leftButton{ if (!_leftButton) { _leftButton = [UIButton buttonWithType:UIButtonTypeSystem]; _leftButton.frame = CGRectMake(0, 0, HAddressToolBar_Button_Width, 50); _leftButton.tag = 100; [_leftButton setTitleColor:[UIColor colorWithRed:219/255.0f green:38/255.0f blue:28/255.0f alpha:1.0f] forState:UIControlStateNormal]; [_leftButton setTitle:@"取消" forState:0]; [_leftButton addTarget:self action:@selector(buttonEvent:) forControlEvents:1<<6]; } return _leftButton; } - (UILabel *)titleLable{ if (!_titleLable) { _titleLable = [[UILabel alloc] init]; _titleLable.hidden = YES; _titleLable.frame = CGRectMake(HAddressToolBar_Button_Width, 0, CGRectGetWidth(self.bounds)-2*HAddressToolBar_Button_Width, 50); _titleLable.textAlignment = 1; } return _titleLable; } - (UIButton *)rightButton{ if (!_rightButton) { _rightButton = [UIButton buttonWithType:UIButtonTypeSystem]; _rightButton.frame = CGRectMake(CGRectGetWidth(self.bounds)-HAddressToolBar_Button_Width,0,HAddressToolBar_Button_Width, 50); _rightButton.tag = 200; [_rightButton setTitleColor:[UIColor colorWithRed:219/255.0f green:38/255.0f blue:28/255.0f alpha:1.0f] forState:UIControlStateNormal]; [_rightButton setTitle:@"完成" forState:0]; [_rightButton addTarget:self action:@selector(buttonEvent:) forControlEvents:1<<6]; } return _rightButton; } - (void)buttonEvent:(UIButton *)button { NSInteger type = 0; if (button.tag == 200) { type = 1; } if (_delegate && [_delegate respondsToSelector:@selector(toolBarDidClickButton:)]) { [_delegate toolBarDidClickButton:type]; } } @end
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
_pickView = [[ZKAddressPickView alloc] init]; _pickView.hideWhenTapGrayView = YES; //_pickView.columns = 2; // 省市二级选择 _pickView.columns = 3; // 省市区三级选择 _pickView.dataArray = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"address" ofType:@"plist"]]; _pickView.pickBlock = ^(NSDictionary *dic) { NSLog(@"所选地址:%@",dic); }; [ _pickView showInView:self.view];