博客> iOS 省份城市选择器
iOS 省份城市选择器
1小时前 评论:0 阅读:497 OS_JJ
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>
            @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>
            /** 地址数据 */
            @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);

        }
收藏
0
sina weixin mail 回到顶部