iOS 省份城市选择器

第一种、微信个人资料修改中设置地区版本(省、市两个页面选择)

DCF5337D168050B35DBAFC708802D2F4.png
    **一、选择省份 MSUAdressController**

        1.MSUAdressController.h 中的代码

            #import <UIKit>

            @interface MSUAdressController : UIViewController
            
            @property (nonatomic , copy) void(^locationSuccessBlock)(NSString *str);
            
            @end

        2.MSUAdressController.h 中的代码

            #import "MSUAdressController.h"
            #import "MSUReviseTableCell.h"
            #import "MSUAddCityController.h"
            
            #import "MSUPrefixHeader.pch"
            #import "MSUHomeNavView.h"
            #import "MSUPathTools.h"
            #import "MSUPermissionTool.h"
            
            /* 地图框架 */
            #import <CoreLocation>
            #import "TQLocationConverter.h"
            #import "ZCChinaLocation.h"
            
            @interface MSUAdressController ()<UITableViewDelegate>
            
            @property (nonatomic , strong) UITableView *tableView;
            
            @property (nonatomic , strong) NSArray *areaArr;
            @property (nonatomic , strong) NSMutableArray *provinceArr;
            
            /// 定位位置按钮
            @property (nonatomic , strong) UILabel *locationLab;
            /// 定位
            @property (nonatomic , strong) CLLocationManager *locationManager;
            /// 得到当前位置的经纬度
            @property (nonatomic , assign) CLLocationCoordinate2D curCoordinate2D;
            
            @end
            
            @implementation MSUAdressController
            
            - (void)viewDidLoad {
                [super viewDidLoad];
                // Do any additional setup after loading the view.
                
                self.view.backgroundColor = BlackColor;
                
                MSUHomeNavView *nav = [[MSUHomeNavView alloc] initWithFrame:NavRect showNavWithNumber:33];
                [self.view addSubview:nav];
                [nav.backArrowBtn addTarget:self action:@selector(backArrowBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                
                UIView *bgView = [[UIView alloc] init];
                bgView.frame = CGRectMake(0, 60, WIDTH, HEIGHT-60);
                bgView.backgroundColor = HEXCOLOR(0xf2f2f2);
                [self.view addSubview:bgView];
                
                UILabel *seeLab = [[UILabel alloc] init];
                seeLab.frame = CGRectMake(14, 25, WIDTH-28, 14);
                seeLab.text = @"定位到的位置";
                seeLab.textColor = HEXCOLOR(0x989898);
                seeLab.font = [UIFont systemFontOfSize:13];
                [bgView addSubview:seeLab];
                
                UIView *bg1View = [[UIView alloc] init];
                bg1View.frame = CGRectMake(0, CGRectGetMaxY(seeLab.frame)+5, WIDTH, 40);
                bg1View.backgroundColor = HEXCOLOR(0xffffff);
                [bgView addSubview:bg1View];
                
                UIImageView *iconIma = [[UIImageView alloc] init];
                iconIma.contentMode = UIViewContentModeScaleAspectFit;
                iconIma.image = [MSUPathTools showImageWithContentOfFileByName:@"WechatIMG1028"];
                [bg1View addSubview:iconIma];
                [iconIma makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(bg1View.top).offset(10);
                    make.left.equalTo(bg1View.left).offset(14);
                    make.width.equalTo(20);
                    make.height.equalTo(20);
                }];
                
            
                self.areaArr = [MSUPathTools getPlistPathWithName:@"areaCity" type:@"plist"];
                self.provinceArr = [NSMutableArray array];
                for (NSInteger i = 0; i < self xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed locationManager.desiredAccuracy=kCLLocationAccuracyBest; xss=removed xss=removed locationManager.distanceFilter=distance; xss=removed> *)locations{
                CLLocation *currentLoction = [locations lastObject];
                /// 得到当前位置的经纬度
                self.curCoordinate2D = currentLoction.coordinate;
                //    NSLog(@"定位直接得到的坐标===%f  %f",_curCoordinate2D.latitude,_curCoordinate2D.longitude);
                [self.locationManager stopUpdatingLocation];
                
                /// 判断当前坐标是否在中国
                BOOL isChina = [[ZCChinaLocation shared] isInsideChina:(CLLocationCoordinate2D){_curCoordinate2D.latitude,_curCoordinate2D.longitude}];
                if (isChina) {
                    /// 转换坐标 不转换会出现偏移
                    _curCoordinate2D = [TQLocationConverter transformFromWGSToGCJ:_curCoordinate2D];
                    
                    //获得地理位置名字
                    [self googleMapAddress];
                }
                
            }
            
            // 获得地理位置名字
            - (NSString *)googleMapAddress{
                __block NSString *addressStr;
                CLGeocoder *geocoder = [[CLGeocoder alloc] init];
                CLLocation *location = [[CLLocation alloc] initWithLatitude:_curCoordinate2D.latitude longitude:_curCoordinate2D.longitude];
                //    NSLog(@"转换后得到的坐标===%f  %f",_curCoordinate2D.latitude,_curCoordinate2D.longitude);
                
                [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark> * _Nullable placemarks, NSError * _Nullable error) {
                    if (error || placemarks.count == 0) {
                        addressStr = @"";
                        NSLog(@"获取定理位置失败");
                    }else{
                        addressStr = [placemarks firstObject].locality;
            //            NSLog(@"------------ %@",addressStr);
                        
            //            NSString *contryStr = [placemarks firstObject].country;
            //            NSLog(@"------------ %@",contryStr);
                        NSString *provinceStr = [[placemarks firstObject].administrativeArea substringWithRange:NSMakeRange(0, 2)];
            //            NSLog(@"------------ %@",provinceStr);
                        
                        NSString *locationCity = [addressStr substringWithRange:NSMakeRange(0, 2) ];
                        self.locationLab.text = [NSString stringWithFormat:@"%@ %@",provinceStr,locationCity];
                        
                        //存储city  用以判断下次app进入city默认显示值
                        [[NSUserDefaults standardUserDefaults] setObject:locationCity forKey:@"location"];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    }
                }];
                return addressStr;
            }
            
            - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
                return self.provinceArr.count;
            }
            
            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
                MSUReviseTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addressCell"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                cell.leftLab.text = self.provinceArr[indexPath.row];
                [cell.line1View remakeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.equalTo(cell.bottom).offset(-1);
                    make.left.equalTo(cell.left).offset(14);
                    make.width.equalTo(WIDTH-14);
                    make.height.equalTo(1);
                }];
                
                return cell;
            }
            
            - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
                NSDictionary *dic = self.areaArr[indexPath.row];
                NSArray *cityArr = dic[self.provinceArr[indexPath.row]];
                MSUAddCityController *city = [[MSUAddCityController alloc] init];
                city.provinceStr = [self.provinceArr[indexPath.row] substringWithRange:NSMakeRange(0, 2)];
                city.cityArr = cityArr;
                [self.navigationController pushViewController:city animated:YES];
            }
            
            - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
                UIView *headerView = [[UIView alloc] init];
                headerView.frame = CGRectMake(0, 0, WIDTH, 20);
                headerView.backgroundColor = HEXCOLOR(0xf2f2f2);
                
                UILabel *allLab = [[UILabel alloc] init];
                allLab.frame = CGRectMake(14, 0, 60, 20);
                allLab.text = @"全部";
                allLab.textColor = HEXCOLOR(0x989898);
                allLab.font = [UIFont systemFontOfSize:13];
                [headerView addSubview:allLab];
                
                return headerView;
            }
            
            - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
                return 20;
            }
            
            
            - (void)backArrowBtnClick:(UIButton *)sender{
                [self.navigationController popViewControllerAnimated:YES];
            }
            
            
            @end

    **二、城市选择 MSUAddCityController**

        1.MSUAddCityController.h 中的代码

            #import <UIKit>

            @interface MSUAddCityController : UIViewController
            
            @property (nonatomic , copy) NSString *provinceStr;
            
            @property (nonatomic , strong) NSArray *cityArr;
            
            @end

        2.MSUAddCityController.m 中的代码

            #import "MSUAddCityController.h"
            #import "MSUReviseInfoController.h"
            
            #import "MSUReviseTableCell.h"
            #import "MSUDanLi.h"
            
            #import "MSUPrefixHeader.pch"
            #import "MSUHomeNavView.h"
            
            @interface MSUAddCityController ()<UITableViewDelegate>
            
            @property (nonatomic , strong) UITableView *tableView;
            
            @end
            
            @implementation MSUAddCityController
            
            - (void)viewDidLoad {
                [super viewDidLoad];
                // Do any additional setup after loading the view.
                self.view.backgroundColor = BlackColor;
                
                MSUHomeNavView *nav = [[MSUHomeNavView alloc] initWithFrame:NavRect showNavWithNumber:33];
                [self.view addSubview:nav];
                [nav.backArrowBtn addTarget:self action:@selector(backArrowBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                
                self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT-60) style:UITableViewStylePlain];
                _tableView.backgroundColor = HEXCOLOR(0xf2f2f2);
                _tableView.dataSource = self;
                _tableView.delegate = self;
                [self.view addSubview:_tableView];
                [_tableView registerClass:[MSUReviseTableCell class] forCellReuseIdentifier:@"addressCell"];
                _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
                _tableView.rowHeight = 40;
            }
            
            - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
                return self.cityArr.count;
            }
            
            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
                MSUReviseTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addressCell"];
                
                cell.leftLab.text = self.cityArr[indexPath.row];
                cell.firstIma.hidden = YES;
                [cell.line1View remakeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.equalTo(cell.bottom).offset(-1);
                    make.left.equalTo(cell.left).offset(14);
                    make.width.equalTo(WIDTH-14);
                    make.height.equalTo(1);
                }];
                
                return cell;
            }
            
            - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
                NSString *cityStr = [self.cityArr[indexPath.row] substringToIndex:2];
                NSString *addStr = [NSString stringWithFormat:@"%@ %@",self.provinceStr,cityStr];
                MSUDanLi *danli = [MSUDanLi shareDanli];
                danli.addStr = addStr;
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[MSUReviseInfoController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                    }
                }
            }
            
            - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
                UIView *headerView = [[UIView alloc] init];
                headerView.frame = CGRectMake(0, 0, WIDTH, 40);
                headerView.backgroundColor = HEXCOLOR(0xf2f2f2);
                
                UILabel *allLab = [[UILabel alloc] init];
                allLab.frame = CGRectMake(14, 20, 60, 20);
                allLab.text = @"全部";
                allLab.textColor = HEXCOLOR(0x989898);
                allLab.font = [UIFont systemFontOfSize:13];
                [headerView addSubview:allLab];
                
                return headerView;
            }
            
            - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
                return 40;
            }
            
            
            #pragma mark - 点击事件
            - (void)backArrowBtnClick:(UIButton *)sender{
                [self.navigationController popViewControllerAnimated:YES];
            }
            
            
            @end

第二种、淘宝版添加收货地址版(三级联动)

6C9903EC0CA50A6299117778F44BD1C4.png
    一、创建继承于UIView的 BLAreaPickerView 类

        1.BLAreaPickerView.h 中的代码

            #import <UIKit/UIKit.h>
            @class BLAreaPickerView;
            @protocol BLPickerViewDelegate <NSObject>
            
            
            @optional
            // 确定按钮点击回调
            - (void)bl_selectedAreaResultWithProvince:(NSString *)provinceTitle
                                                 city:(NSString *)cityTitle
                                                 area:(NSString *)areaTitle;
            // 取消按钮点击回调
            - (void)bl_cancelButtonClicked;
            @end
            
            typedef void(^SelectBlock)(NSString *provinceTitle, NSString *cityTitle, NSString *areaTitle);
            
            @interface BLAreaPickerView : UIView
            /** 标题大小 */
            @property (nonatomic, strong)UIFont  *titleFont;
            /** 选择器背景颜色 */
            @property (nonatomic, strong)UIColor *pickViewBackgroundColor;
            /** 选择器头部视图颜色 */
            @property (nonatomic, strong)UIColor *topViewBackgroundColor;
            /** 取消按钮颜色 */
            @property (nonatomic, strong)UIColor *cancelButtonColor;
            /** 确定按钮颜色 */
            @property (nonatomic, strong)UIColor *sureButtonColor;
            
            /** 选择器代理 */
            @property (nonatomic, weak) id<BLPickerViewDelegate> pickViewDelegate;
            
            /** 显示选择器 */
            - (void)bl_show;
            // 确定按钮点击回调
            - (void)sureButtonClickBlock:(SelectBlock)newBlock;
            
            @end

        2.BLAreaPickerView.m 中的代码

            
            #import "BLAreaPickerView.h"
            
            @interface BLAreaPickerView()<UIPickerViewDelegate, UIPickerViewDataSource,UIGestureRecognizerDelegate>
            /** 地址数据 */
            @property (nonatomic, strong) NSArray *areaArray;
            /** pickView */
            @property (nonatomic, strong) UIPickerView *pickView;
            /** 顶部视图 */
            @property (nonatomic, strong) UIView *topView;
            /** 取消按钮 */
            @property (nonatomic, strong) UIButton *cancelButton;
            /** 确定按钮 */
            @property (nonatomic, strong) UIButton *sureButton;
            
            @property (nonatomic, copy) SelectBlock tureBlock;
            
            @end
            
            static const CGFloat topViewHeight = 30;
            static const CGFloat buttonWidth = 60;
            static const CGFloat animationDuration = 0.3;
            #define BL_ScreenW  [[UIScreen mainScreen] bounds].size.width
            #define BL_ScreenH  [[UIScreen mainScreen] bounds].size.height
            typedef enum : NSUInteger {
                BLComponentTypeProvince = 0, // 省
                BLComponentTypeCity,         // 市
                BLComponentTypeArea,         // 区
            } BLComponentType;
            
            @implementation BLAreaPickerView
            {
                NSInteger _provinceSelectedRow;
                NSInteger _citySelectedRow;
                NSInteger _areaSelectedRow;
                
                NSString *_selectedProvinceTitle;
                NSString *_selectedCityTitle;
                NSString *_selectedAreaTitle;
                
                CGRect _pickViewFrame;
            }
            
            #pragma mark - - load
            - (instancetype)initWithFrame:(CGRect)frame {
                self = [super initWithFrame:frame];
                if (self) {
                    [self bl_initData:frame];
                    [self bl_initSubviews];
                }
                return self;
            }
            
            /** 初始化子视图 */
            - (void)bl_initSubviews{
                
                [self addSubview:self.topView];
                [self addSubview:self.pickView];
                [self.topView addSubview:self.cancelButton];
                [self.topView addSubview:self.sureButton];
            }
            
            /** 初始化数据 */
            - (void)bl_initData:(CGRect)frame{
                _pickViewFrame = frame;
                
                self.frame = CGRectMake(0, 0, BL_ScreenW, BL_ScreenH);
                self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bl_dismiss)];
                tap.delegate = self;
                [self addGestureRecognizer:tap];
                
                _provinceSelectedRow = 0;
                _citySelectedRow = 0;
                _areaSelectedRow = 0;
                
                NSString *plistStr = [[NSBundle mainBundle] pathForResource:@"areaArray" ofType:@"plist"];
                _areaArray = [[NSArray alloc] initWithContentsOfFile:plistStr];
            }
            
            #pragma mark - - get
            - (UIPickerView *)pickView{
                if (!_pickView) {
                    _pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_topView.frame), BL_ScreenW, _pickViewFrame.size.height)];
                    _pickView.dataSource = self;
                    _pickView.delegate = self;
                    _pickView.backgroundColor = [UIColor colorWithRed:244.0/255 green:244.0/255 blue:244.0/255 alpha:1];
                }
                return _pickView;
            }
            
            - (UIView *)topView{
                if (!_topView) {
                    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, BL_ScreenH, BL_ScreenW, topViewHeight)];
                    _topView.backgroundColor =  [UIColor colorWithRed:244.0/255 green:244.0/255 blue:244.0/255 alpha:1];
                    
                    UILabel *seeLab = [[UILabel alloc] init];
                    seeLab.frame = CGRectMake(BL_ScreenW*0.5-75, 10, 150, 20);
                    seeLab.textAlignment = NSTextAlignmentCenter;
                    seeLab.text = @"请选择省市";
                    seeLab.textColor = [UIColor blackColor];
                    seeLab.font = [UIFont systemFontOfSize:14];
                    [_topView addSubview:seeLab];
                }
                return _topView;
            }
            
            - (UIButton *)cancelButton{
                if (!_cancelButton) {
                    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    _cancelButton.frame = CGRectMake(0, 0, buttonWidth, topViewHeight);
                    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
                    [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
                    [_cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                }
                return _cancelButton;
            }
            
            - (UIButton *)sureButton{
                if (!_sureButton) {
                    _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    _sureButton.frame = CGRectMake(self.frame.size.width - buttonWidth, 0, buttonWidth, topViewHeight);
                    [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
                    [_sureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [_sureButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
                    [_sureButton addTarget:self action:@selector(sureButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                }
                return _sureButton;
            }
            
            #pragma mark - - set
            - (void)setPickViewBackgroundColor:(UIColor *)pickViewBackgroundColor{
                self.pickView.backgroundColor = pickViewBackgroundColor;
            }
            
            - (void)setTopViewBackgroundColor:(UIColor *)topViewBackgroundColor{
                self.topView.backgroundColor = topViewBackgroundColor;
            }
            
            - (void)setCancelButtonColor:(UIColor *)cancelButtonColor{
                [self.cancelButton setTitleColor:cancelButtonColor forState:UIControlStateNormal];
            }
            
            - (void)setSureButtonColor:(UIColor *)sureButtonColor{
                [self.sureButton setTitleColor:sureButtonColor forState:UIControlStateNormal];
            }
            
            #pragma mark - show,dismiss
            
            - (void)bl_show{
                [[UIApplication sharedApplication].keyWindow addSubview:self];
                [UIView animateWithDuration:animationDuration animations:^{
                    CGRect tempRect = _topView.frame;
                    tempRect.origin.y = BL_ScreenH - topViewHeight - _pickViewFrame.size.height;
                    _topView.frame = tempRect;
                    tempRect = _pickViewFrame;
                    tempRect.origin.y = CGRectGetMaxY(_topView.frame);
                    _pickView.frame = tempRect;
                }];
            }
            
            - (void)bl_dismiss{
                [UIView animateWithDuration:animationDuration animations:^{
                    CGRect tempRect = _topView.frame;
                    tempRect.origin.y = BL_ScreenH;
                    _topView.frame = tempRect;
                    tempRect = _pickViewFrame;
                    tempRect.origin.y = CGRectGetMaxY(_topView.frame);
                    _pickView.frame = tempRect;
                } completion:^(BOOL finished) {
                    [self removeFromSuperview];
                    
                }];
            }
            
            #pragma mark - - Button Action
            - (void)cancelButtonClicked:(UIButton *)sender{
                
                if (self.pickViewDelegate &&
                    [self.pickViewDelegate respondsToSelector:@selector(bl_cancelButtonClicked)]) {
                    [self.pickViewDelegate bl_cancelButtonClicked];
                }
                [self bl_dismiss];
            }
            
            - (void)sureButtonClicked:(UIButton *)sender{
                
                _selectedProvinceTitle = [self pickerView:_pickView titleForRow:_provinceSelectedRow forComponent:0];
                _selectedCityTitle = [self pickerView:_pickView titleForRow:_citySelectedRow forComponent:1];
                _selectedAreaTitle = [self pickerView:_pickView titleForRow:_areaSelectedRow forComponent:2];
                
                if (self.pickViewDelegate &&
                    [self.pickViewDelegate respondsToSelector:@selector(bl_selectedAreaResultWithProvince:city:area:)]) {
                    [self.pickViewDelegate bl_selectedAreaResultWithProvince:_selectedProvinceTitle
                                                                        city:_selectedCityTitle
                                                                        area:_selectedAreaTitle];
                }
                
                if (_tureBlock) {
                    _tureBlock(_selectedProvinceTitle, _selectedCityTitle, _selectedAreaTitle);
                }
                
                [self bl_dismiss];
            }
            
            - (void)sureButtonClickBlock:(SelectBlock)newBlock {
                _tureBlock =newBlock;
            }
            
            #pragma mark - - UIPickerViewDelegate,UIPickerViewDataSource
            - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
                return 3;
            }
            
            - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
                switch (component) {
                    case BLComponentTypeProvince:
                        return _areaArray.count;
                        break;
                    case BLComponentTypeCity:
                        return [[[_areaArray objectAtIndex:_provinceSelectedRow] objectForKey:@"citylist" ] count];
                        break;
                    case BLComponentTypeArea:
                        return [[[[[_areaArray objectAtIndex:_provinceSelectedRow] objectForKey:@"citylist" ] objectAtIndex:_citySelectedRow] objectForKey:@"arealist"] count];
                        break;
                    default:
                        return _areaArray.count;
                        break;
                }
            }
            
            - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
                
                NSDictionary *provinceDic = [_areaArray objectAtIndex:_provinceSelectedRow];
                NSArray *cityArr = [provinceDic objectForKey:@"citylist"];
                
                switch (component) {
                    case BLComponentTypeProvince:
                        return [_areaArray[row] objectForKey:@"provinceName"];
                        break;
                    case BLComponentTypeCity:{
                        NSDictionary *cityDic = [cityArr objectAtIndex:row];
                        return [cityDic objectForKey:@"cityName"];
                        break;
                    }
                    case BLComponentTypeArea:{
                        NSDictionary *areaDic = [cityArr objectAtIndex:_citySelectedRow];
                        NSArray *areaArr = [areaDic objectForKey:@"arealist"];
                        return [areaArr[row] objectForKey:@"areaName"];
                        break;
                    }
                    default:
                        return [_areaArray[row] objectForKey:@"provinceName"];
                        break;
                }
            }
            
            - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
                switch (component) {
                    case BLComponentTypeProvince:{
                        _provinceSelectedRow = row;
                        _citySelectedRow = 0;
                        _areaSelectedRow = 0;
                        [pickerView selectRow:0 inComponent:1 animated:NO];
                        [pickerView selectRow:0 inComponent:2 animated:NO];
                        break;
                    }
                    case BLComponentTypeCity:{
                        _citySelectedRow = row;
                        _areaSelectedRow = 0;
                        [pickerView selectRow:0 inComponent:2 animated:NO];
                        break;
                    }
                    case BLComponentTypeArea:
                        _areaSelectedRow = row;
                        break;
                    default:
                        _provinceSelectedRow = row;
                        break;
                }
                [pickerView reloadAllComponents];
            }
            
            - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
                return self.frame.size.width / 3;
            }
            
            - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
                
                UILabel* pickerLabel = (UILabel*)view;
                if (!pickerLabel){
                    pickerLabel = [[UILabel alloc] init];
                    pickerLabel.adjustsFontSizeToFitWidth = YES;
                    [pickerLabel setTextAlignment:NSTextAlignmentCenter];
                    [pickerLabel setBackgroundColor:[UIColor clearColor]];
                    [pickerLabel setFont:_titleFont ? _titleFont : [UIFont systemFontOfSize:14]];
                }
                pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
                return pickerLabel;
            }
            
            
            - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
                if ([touch.view isDescendantOfView:_pickView]||[touch.view isDescendantOfView:_topView]) {
                    //子视图,或本身
                    return NO;
                }
                return YES;
            }


    二、点击按钮 显示BLAreaPickerView,

              [self.view endEditing:YES];
                self.pickView = [[BLAreaPickerView alloc] initWithFrame:CGRectMake(0, 128, WIDTH, HEIGHT-128-128)];
                _pickView.pickViewDelegate = self;
                [_pickView bl_show];

    三、遵循BLAreaPickerView代理

        /* pickview代理 */
        - (void)bl_selectedAreaResultWithProvince:(NSString *)provinceTitle city:(NSString *)cityTitle area:(NSString *)areaTitle{
            self.areaStr = [NSString stringWithFormat:@"%@%@%@",provinceTitle,cityTitle,areaTitle];
            NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:2 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath1, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
            NSLog(@"%@",_areaStr);
            
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容