美文网首页iOS中国iOS Developer
iOS 地址选择器(省市区)

iOS 地址选择器(省市区)

作者: GF极客天涯 | 来源:发表于2017-05-12 16:16 被阅读1230次

    近期因公司需求需要,封装了一个省市区选择器,以供大家参考,欢迎提出宝贵意见!

    Untitled.gif

    使用方法

    • 1遵守代理
    • 2 实现确定代理方法
    • 3 实现取消代理方法
    1.确定代理方法
    - (void)GFAddressPickerWithProvince:(NSString *)province
                                              city:(NSString *)city area:(NSString *)area;
    
    2.取消代理方法
    
    - (void)GFAddressPickerCancleAction;
    

    使用极其简单,直接贴代码

    #import "ViewController.h"
    #import "GFAddressPicker.h"
    @interface ViewController ()<GFAddressPickerDelegate>
    @property (nonatomic, strong) GFAddressPicker *pickerView;
    @property (nonatomic,strong) UILabel *lab;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(50 , 100, 275,50);
        [button setTitle:@"选择" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor cyanColor];
        [self.view addSubview:button];
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.lab = [[UILabel alloc]initWithFrame:CGRectMake(50 , 200, 270, 50)];
        self.lab.textAlignment = NSTextAlignmentCenter;
        
        _lab.backgroundColor = [UIColor redColor];
    
        [self.view addSubview:_lab];
    
    }
    - (void)btnAction:(UIButton *)sender
    {
     
        self.pickerView = [[GFAddressPicker alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        [self.pickerView updateAddressAtProvince:@"河南省" city:@"郑州市" town:@"金水区"];
        self.pickerView.delegate = self;
        self.pickerView.font = [UIFont boldSystemFontOfSize:18];
        [self.view addSubview:self.pickerView];
    }
    - (void)GFAddressPickerCancleAction
    {
           [self.pickerView removeFromSuperview];
    }
    
    - (void)GFAddressPickerWithProvince:(NSString *)province
                                   city:(NSString *)city area:(NSString *)area
    {
        [self.pickerView removeFromSuperview];
    
        self.lab.text = [NSString stringWithFormat:@"%@  %@  %@",province,city,area];
        
        NSLog(@"%@  %@  %@",province,city,area);
    }
    
    

    GFAddressPicker.h

    #import <UIKit/UIKit.h>
    @protocol GFAddressPickerDelegate <NSObject>
    @optional
    /** 代理方法返回省市区*/
    - (void)GFAddressPickerWithProvince:(NSString *)province
                                              city:(NSString *)city area:(NSString *)area;
    /** 取消代理方法*/
    - (void)GFAddressPickerCancleAction;
    @end
    @interface GFAddressPicker : UIView
    
    /** 省 */
    @property (nonatomic, strong) NSString *province;
    /** 市 */
    @property (nonatomic, strong) NSString *city;
    /** 区 */
    @property (nonatomic, strong) NSString *area;
    @property (nonatomic, weak) id<GFAddressPickerDelegate> delegate;
    - (void)updateAddressAtProvince:(NSString *)province city:(NSString *)city town:(NSString *)town;
    /** 内容字体 */
    @property (nonatomic, strong) UIFont *font;
    @end
    

    GFAddressPicker.m

    //
    //  GFAddressPicker.m
    //  地址选择器
    //
    //  Created by 1暖通商城 on 2017/5/10.
    //  Copyright © 2017年 1暖通商城. All rights reserved.
    //
    
    #import "GFAddressPicker.h"
    
    
    @interface GFAddressPicker ()<UIPickerViewDelegate,UIPickerViewDataSource>
    @property (strong, nonatomic) NSDictionary *pickerDic;
    @property (strong, nonatomic) NSArray *selectedArray;
    @property (strong, nonatomic) NSArray *provinceArray;
    @property (strong, nonatomic) NSArray *cityArray;
    @property (strong, nonatomic) NSArray *townArray;
    @property (strong, nonatomic) UIPickerView *pickView;
    @end
    @implementation GFAddressPicker
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            [self getAddressInformation];
            [self setBaseView];
        }
        return self;
    }
    
    - (void)getAddressInformation {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
        self.pickerDic = [[NSDictionary alloc] initWithContentsOfFile:path];
        self.provinceArray = [self.pickerDic allKeys];
        self.selectedArray = [self.pickerDic objectForKey:[[self.pickerDic allKeys] objectAtIndex:0]];
        if (self.selectedArray.count > 0) {
            self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
        }
        if (self.cityArray.count > 0) {
            self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
        }
    }
    
    - (void)setBaseView {
        CGFloat height = self.frame.size.height;
        CGFloat width = self.frame.size.width;
        UIColor *color = [UIColor colorWithRed:242/255.0 green:243/255.0 blue:249/255.0 alpha:1];
        UIColor *btnColor = [UIColor colorWithRed:65.0/255 green:164.0/255 blue:249.0/255 alpha:1];
        UIView *selectView = [[UIView alloc] initWithFrame:CGRectMake(0, height - 210, width, 30)];
        selectView.backgroundColor = color;
        UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancleBtn setTitle:@"取消" forState:0];
        [cancleBtn setTitleColor:btnColor forState:0];
        cancleBtn.frame = CGRectMake(0, 0, 60, 40);
        [cancleBtn addTarget:self action:@selector(dateCancleAction) forControlEvents:UIControlEventTouchUpInside];
        [selectView addSubview:cancleBtn];
        
        UIButton *ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [ensureBtn setTitle:@"确定" forState:0];
        [ensureBtn setTitleColor:btnColor forState:0];
        ensureBtn.frame = CGRectMake(width - 60, 0, 60, 40);
        [ensureBtn addTarget:self action:@selector(dateEnsureAction) forControlEvents:UIControlEventTouchUpInside];
        [selectView addSubview:ensureBtn];
        [self addSubview:selectView];
        
        self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, height - 180 , width,  180)];
        self.pickView.delegate   = self;
        self.pickView.dataSource = self;
        self.pickView.backgroundColor = color;
        [self addSubview:self.pickView];
        [self.pickView reloadAllComponents];
        [self updateAddress];
        
    }
    
    - (void)updateAddressAtProvince:(NSString *)province city:(NSString *)city town:(NSString *)town {
        self.province = province;
        self.city = city;
        self.area = town;
        if (self.province) {
            for (NSInteger i = 0; i < self.provinceArray.count; i++) {
                NSString *city = self.provinceArray[i];
                NSInteger select = 0;
                if ([city isEqualToString:self.province]) {
                    select = i;
                    [self.pickView selectRow:i inComponent:0 animated:YES];
                    break;
                }
            }
            self.cityArray = [self.pickerDic[self.province][0] allKeys];
            for (NSInteger i = 0; i < self.cityArray.count; i++) {
                NSString *city = self.cityArray[i];
                if ([city isEqualToString:self.city]) {
                    [self.pickView selectRow:i inComponent:1 animated:YES];
                    break;
                }
            }
            self.townArray = self.pickerDic[self.province][0][self.city];
            for (NSInteger i = 0; i < self.townArray.count; i++) {
                NSString *town = self.townArray[i];
                if ([town isEqualToString:self.area]) {
                    [self.pickView selectRow:i inComponent:2 animated:YES];
                    break;
                }
            }
        }
        [self.pickView reloadAllComponents];
        [self updateAddress];
    }
    
    - (void)dateCancleAction {
        if (self.delegate && [self.delegate respondsToSelector:@selector(GFAddressPickerCancleAction)]) {
            [self.delegate GFAddressPickerCancleAction];
        }
    }
    
    - (void)dateEnsureAction {
        if (self.delegate && [self.delegate respondsToSelector:@selector(GFAddressPickerWithProvince:city:area:)]) {
            [self.delegate GFAddressPickerWithProvince:self.province city:self.city area:self.area];
        }
    }
    
    - (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.textAlignment = NSTextAlignmentCenter;
            [pickerLabel setBackgroundColor:[UIColor clearColor]];
            [pickerLabel setFont:self.font?:[UIFont boldSystemFontOfSize:14]];
        }
        pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
        return pickerLabel;
    }
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        return 3;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        if (component == 0) {
            return self.provinceArray.count;
        } else if (component == 1) {
            return self.cityArray.count;
        } else {
            return self.townArray.count;
        }
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        if (component == 0) {
            return [self.provinceArray objectAtIndex:row];
        } else if (component == 1) {
            return [self.cityArray objectAtIndex:row];
        } else {
            return [self.townArray objectAtIndex:row];
        }
    }
    
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
        return self.frame.size.width / 3;
    }
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        if (component == 0) {
            self.selectedArray = [self.pickerDic objectForKey:[self.provinceArray objectAtIndex:row]];
            if (self.selectedArray.count > 0) {
                self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
            } else {
                self.cityArray = @[];
            }
            if (self.cityArray.count > 0) {
                self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
            } else {
                self.townArray = @[];
            }
            [pickerView reloadComponent:1];
            [pickerView selectedRowInComponent:1];
            [pickerView reloadComponent:2];
            [pickerView selectedRowInComponent:2];
        }
        if (component == 1) {
            self.selectedArray = [self.pickerDic objectForKey:[self.provinceArray objectAtIndex:[self.pickView selectedRowInComponent:0]]];
            NSDictionary *dic = self.selectedArray.firstObject;
            NSString *stirng = self.cityArray[row];
            for (NSString *string in dic.allKeys) {
                if ([stirng isEqualToString:string]) {
                    self.townArray = dic[string];
                }
            }
            [pickerView reloadComponent:2];
            [pickerView selectedRowInComponent:2];
        }
        if (component == 2) {
            [pickerView reloadComponent:2];
            [pickerView selectedRowInComponent:2];
        }
        [self updateAddress];
    }
    
    - (void)updateAddress {
        self.province = [self.provinceArray objectAtIndex:[self.pickView selectedRowInComponent:0]];
        self.city  = [self.cityArray objectAtIndex:[self.pickView selectedRowInComponent:1]];
        self.area  = [self.townArray objectAtIndex:[self.pickView selectedRowInComponent:2]];
    }
    
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    
    @end
    
    

    下载地址

    相关文章

      网友评论

      • zxlaikaifa:您好,中间刚进入时为什么匹配不了深圳市?
      • logan丶:找遍了所有的选择器, 都存在一个问题。。数据太旧了, 很多地区名称已经不叫这个了,比如延庆县, 现在叫区了。。
      • 故事愚酒:提出个bug:选择省、市、县后,比如选择第二个市、第二个县,那么此时再滚动省,出现市为第二个,县的数组为第一个市中的所有县,
        self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
        永远匹配的是第一个市的,这里有问题。
      • 随心_追梦:你好,想问一下这种gif图是如何生成的?
        GF极客天涯:@随心_追梦 不客气
        随心_追梦:@GF极客天涯 谢谢
        GF极客天涯:@随心_追梦 有个gif录屏工具

      本文标题:iOS 地址选择器(省市区)

      本文链接:https://www.haomeiwen.com/subject/rqzztxtx.html