iOS:城市级联列表的使用

1、介绍:

现在越来越多的项目都用到了地址,尤其是电商O2O的购物平台,我之前做的教育产品和电商产品都用到了,而实现地址的设置用到的技术就是城市级联列表,即普遍的做法就是自定义选择器控件UIPickerView,然后从本地整理的城市plist文件获取省份、城市、县镇三级级联。现在整理一下,分享给大家。

 

2、效果图如下:

 

3、用法:

(1)导入本地城市级联文件:provincecity.plist.zip

(2)导入DDMeZonePickerView.h头文件,在控制器通过[UIView Animation]的动画控制创建的DDMeZonePickerView视图对象的frame让其显示或隐藏即可,被选择的城市然后通过block进行回传。

 

4、废话不多说,实现代码如下:

DDMeZonePickerView.h

//  DDMeZonePickerView.h
//  BiaoJiePay
//
//  Created by 夏远全 on 16/11/19.
//  Copyright © 2016年 广州东德科技. All rights reserved.
//

#import <UIKit/UIKit.h>

/**
 *  声明block
 */
typedef void (^adressBlock)(NSString *adress);

@interface DDMeZonePickerView : UIView
/**
 *  定义block属性
 */
@property (copy,nonatomic)adressBlock adressBlock;
/**
 *  传递地址
 */
-(void)chooseAdressBlock:(adressBlock)adressBlock;

@end

DDMeZonePickerView.m

//
//  DDMeZonePickerView.m
//  BiaoJiePay
//
//  Created by 夏远全 on 16/11/19.
//  Copyright © 2016年 广州东德科技. All rights reserved.
//

#import "DDMeZonePickerView.h"

@interface DDMeZonePickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

/** picker选择器*/
@property (strong, nonatomic)UIPickerView *customPicker;
/**
 *  原始省份数组
 */
@property (strong, nonatomic)NSArray *provinceArray;
/**
 *  原始城市数组
 */
@property (strong, nonatomic)NSArray *cityArray;
/**
 *  原始区镇数组
 */
@property (strong, nonatomic)NSArray *townArray;
/**
 *  选择省份字典
 */
@property (strong, nonatomic)NSDictionary *selectedProvinceDic;
/**
 *  选择城市字典
 */
@property (strong, nonatomic)NSDictionary *selectedCityDic;
/**
 *  确认
 */
@property (strong,nonatomic)UIButton *sureButton;
/**
 *  取消
 */
@property (strong,nonatomic)UIButton *cancelButton;

@end

@implementation DDMeZonePickerView
/**
 *  懒加载
 */
-(UIPickerView *)customPicker{
    if (!_customPicker) {
        _customPicker = [[UIPickerView alloc]init];
        _customPicker.dataSource = self;
        _customPicker.delegate = self;
        _customPicker.backgroundColor = XYQColor(153, 153, 153);
    }
    return _customPicker;
}
-(NSArray *)provinceArray{
    if (!_provinceArray) {
        _provinceArray = [NSArray array];
    }
    return _provinceArray;
}
-(NSArray *)cityArray{
    if (!_cityArray) {
        _cityArray = [NSArray array];
    }
    return _cityArray;
}
-(NSArray *)townArray{
    if (!_townArray) {
        _townArray = [NSArray array];
    }
    return _townArray;
}
-(NSDictionary *)selectedProvinceDic{
    if (!_selectedProvinceDic) {
        _selectedProvinceDic = [NSDictionary dictionary];
    }
    return _selectedProvinceDic;
}
-(NSDictionary *)selectedCityDic{
    if (!_selectedCityDic) {
        _selectedCityDic = [NSDictionary dictionary];
    }
    return _selectedCityDic;
}

/**
 *  初始化
 */
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self getPickerData];
        self.customPicker.frame = CGRectMake(0, 0, SCREEN_WIDTH, frame.size.height);
        [self addSubview:self.customPicker];
    }
    return self;
}

/**
 *  从本地文件获取数据
 */
- (void)getPickerData{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"provincecity" ofType:@"plist"];
    self.provinceArray = [NSArray arrayWithContentsOfFile:path]; //所有的省份
    
    self.selectedProvinceDic = self.provinceArray[0];
    self.cityArray = [self.selectedProvinceDic objectForKey:@"city"];//所有的城市
    
    self.selectedCityDic = self.cityArray[0];
    self.townArray = [self.selectedCityDic objectForKey:@"area"];//所有的区/县
}

#pragma mark UIPickerViewDataSource
//设置有多少列
-(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;
    }
}

#pragma mark - UIPickerViewDelegate
//自定义字体大小
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
    UILabel *myView = nil;
    if (component == 0) {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)] ;
        myView.text = [[self.provinceArray objectAtIndex:row] objectForKey:@"name"];
        
    }else if(component == 1){
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, (SCREEN_WIDTH-100)/2, 30)];
        myView.text = [[self.cityArray objectAtIndex:row] objectForKey:@"name"];
        
    }else{
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, (SCREEN_WIDTH-100)/2-30, 30)];
        myView.text = [[self.townArray objectAtIndex:row] objectForKey:@"name"];
    }
    myView.textAlignment = NSTextAlignmentCenter;
    myView.font = [UIFont systemFontOfSize:17]; //用label来设置字体大小
    myView.backgroundColor = [UIColor whiteColor];
    
    return myView;
}

//设置宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return SCREEN_WIDTH/3;
}


//返回行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 39;
}

//通过代理方法didSelectRows获取数据
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        
        //获取省份选中行
        self.selectedProvinceDic = self.provinceArray[row];
        self.cityArray = [self.selectedProvinceDic objectForKey:@"city"];//所有的城市
        self.selectedCityDic = self.cityArray[0];
        self.townArray = [self.selectedCityDic objectForKey:@"area"];//所有的区/县
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        
    }
    if (component == 1) {
        
        //获取城市选中行
        NSInteger cityselectedRow = [pickerView selectedRowInComponent:1];
        self.selectedCityDic = self.cityArray[cityselectedRow];
        self.townArray = [self.selectedCityDic objectForKey:@"area"];//所有的区/县
        [pickerView reloadComponent:2];
    }
    
    //获取选中的省
    NSString *provice = [NSString stringWithFormat:@"%@",[[self.provinceArray objectAtIndex:[pickerView selectedRowInComponent:0]] objectForKey:@"name"]];
    //获取选中的市
    NSString *city = [NSString stringWithFormat:@"%@",[[self.cityArray objectAtIndex:[pickerView selectedRowInComponent:1]] objectForKey:@"name"]];
    //获取选中的县
    NSString *town = [NSString stringWithFormat:@"%@",[[self.townArray objectAtIndex:[pickerView selectedRowInComponent:2]] objectForKey:@"name"]];
    
    //赋值
    NSString *adressText = [NSString stringWithFormat:@"%@%@%@",provice,city,town];
    self.adressBlock(adressText);
}

/**
 *  地址传值
 */
-(void)chooseAdressBlock:(void (^)(NSString *))adressBlock{
    if (adressBlock) {
        self.adressBlock = [adressBlock copy];
    }
}@end

在ViewController.h懒加载选择器调用block取值:

/**
 *  选择器
 */
-(DDMeZonePickerView *)zonePickerView{
    if (!_zonePickerView) {
        _zonePickerView = [[DDMeZonePickerView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 240)];
        [_zonePickerView chooseAdressBlock:^(NSString *adress) {
            self.updateField.text = adress;
        }];
    }
    return _zonePickerView;
}

本人原创,欢迎大家分享,转载需注明出处

posted @ 2016-11-19 16:58  XYQ全哥  阅读(1470)  评论(0编辑  收藏  举报