ios省市区显示到PickerView

非常实用的两个省市区的PList文件
附上下载地址
http://download.csdn.net/download/wangxiaoertedaye/9653508

定义模型
Model.h

#import 

@interface AddressesModel : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *sub;
@property (nonatomic, strong) NSString *zipcode;

@property (nonatomic, strong) NSMutableArray *subclass;
+(instancetype)initWithDictionary:(NSDictionary *)dic;
@end

Model.m

#import "AddressesModel.h"

@implementation AddressesModel

-(instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

+ (instancetype)initWithDictionary:(NSDictionary *)dic
{
    AddressesModel *model = [[self alloc] init];
    [model setValuesForKeysWithDictionary:dic];
    
    return model;
}

@end

实现部分

@interface EditOrCreatAddressViewController ()
{
    NSInteger _selProvinceIndex;
    NSInteger _selCityIndex;
    NSInteger _selDistrictIndex;
    
    UILabel *textViewLabel;
    UIView *blackView;
    UIPickerView *pikerView;
    
    UILabel *areaContentL;
}

@property (strong, nonatomic)NSMutableArray *addressArray;
@end
//懒加载
- (NSArray *)addressArray
{
    if (!_addressArray) {
        
        _addressArray = [[NSMutableArray alloc] initWithCapacity:42];
        NSString *dictPath = [[NSBundle mainBundle] pathForResource:@"address.plist" ofType:nil];
        NSArray * provinces = [NSArray arrayWithContentsOfFile:dictPath];
        
        for (NSDictionary *provinceDict in provinces) {
            AddressesModel *provinceModel = [AddressesModel initWithDictionary:provinceDict];
            [_addressArray addObject:provinceModel];
            
            provinceModel.subclass = [[NSMutableArray alloc] initWithCapacity:42];
            
            for (NSDictionary *cityDict in provinceModel.sub) {
                AddressesModel *cityMolel = [AddressesModel initWithDictionary:cityDict];
                [provinceModel.subclass addObject:cityMolel];
                
                cityMolel.subclass = [[NSMutableArray alloc] initWithCapacity:42];
                
                for (NSString * str in cityMolel.sub) {
                    [cityMolel.subclass addObject:str];
                }
            }
        }
        
        NSLog(@"addressArray= %@",_addressArray);
        [pikerView reloadAllComponents];
        
    }
    
    return _addressArray;
}
//    地区选择框
    blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height)];
    blackView.backgroundColor = [UIColor blackColor];
    blackView.alpha = 0.5;
    blackView.hidden = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissWindow)];
    [blackView addGestureRecognizer:tap];
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:blackView];
    
    pikerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kScreen_Height, kScreen_Width, 216)];
    pikerView.backgroundColor = RGBCOLOR(245, 245, 245);
    pikerView.hidden = YES;
    pikerView.dataSource = self;
    pikerView.delegate = self;
    [window addSubview:pikerView];
    [self pickerView:pikerView didSelectRow:0 inComponent:0];
#pragma mark PickerView
#pragma mark --UIPickerView选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    //    NSLog(@"Row%zd ---Component %zd",row, component);
    if (component == 0) {//第一列省的选中改变后,就要更新第二列数据
        //        //不管之前第二列选中第几行,重新刷新数据后,都显示每二列的第一行
        _selProvinceIndex = row;
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView reloadComponent:1];
        
        [pickerView selectRow:0 inComponent:2 animated:YES];
        [pickerView reloadComponent:2];
    }
    else if (component == 1) {
        _selCityIndex = row;
        [pickerView selectRow:0 inComponent:2 animated:YES];
        [pickerView reloadComponent:2];
        
    }else{
        _selDistrictIndex = row;
    }
    
    AddressesModel *provinceModel = [self.addressArray objectAtIndex:_selProvinceIndex];
    
    //省名
    NSString *provinceName = provinceModel.name;
    
    //城市名
    AddressesModel *cityModel = provinceModel.subclass[_selCityIndex];
    NSString *cityName = cityModel.name;
    //街道名
    NSString *districtName = cityModel.subclass[_selDistrictIndex];
    
    [areaContentL setText:[NSString stringWithFormat:@"%@%@%@",provinceName,cityName,districtName]];
    
}

#pragma mark -- UIPickerView数据源和代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {//省分
        return self.addressArray.count;
    }else if (component == 1){
        //判断省份选中的是哪一个
        NSInteger rowIndex = [pickerView selectedRowInComponent:0];
        
        if(rowIndex>=0 && self.addressArray.count>rowIndex)
        {
            AddressesModel * model = self.addressArray[rowIndex];
            return model.subclass.count;
        }
        else
            return 0;
    }else{
        
        NSInteger rowIndex = [pickerView selectedRowInComponent:0];
        NSInteger rowIndex1 = [pickerView selectedRowInComponent:1];
        AddressesModel * model = self.addressArray[rowIndex];
        
        
        if(rowIndex1>=0 && model.subclass.count>rowIndex1)
        {
           AddressesModel * cityModel = model.subclass[rowIndex1];
            NSLog(@"sub.count == %zd",cityModel.sub.count);
            return cityModel.subclass.count;
        }
        else
            return 0;
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
        return 34;
}

#pragma mark --UIPickerView显示数据
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *lbl = [UILabel new];
    lbl.frame = CGRectMake(kScreen_Width/3*row, 0, kScreen_Width/3, 45);
    lbl.textColor = [UIColor blackColor];
    lbl.font = [UIFont boldSystemFontOfSize:15];
    
    lbl.textAlignment = NSTextAlignmentCenter;
    if(component == 0){//显示省份的名字
        //对应列行的省份
        if (self.addressArray.count == 0) {
            return nil;
        }
        AddressesModel * model = self.addressArray[row];
        lbl.text = model.name;
    }
    else if(component == 1){
        //获取选中的城市,显示城市名字
        NSInteger rowIndex = [pickerView selectedRowInComponent:0];
        AddressesModel * model = self.addressArray[rowIndex];
        if (self.addressArray.count != 0 && model.sub != NULL && row  row) {
                lbl.text = cityModel.subclass[row];
            }
        }
    }
    return lbl;
}

你可能感兴趣的:(ios省市区显示到PickerView)