一、简介
该文主要实现的是省市区的选择,可在个人修改地址的地方使用。
二、需要的东西
制作这个首先需要一个area.plist文件,该文件中保存这所有的关于省市区的信息,下载地址:http://pan.baidu.com/s/1qWFPf8g。
三、制作原理
通过一个UIPickerView来显示,将UIPickerView分成三部分,省、市、区。进而加载数据。
四、实现效果
图片展示:
五、具体实现步骤:
1、首先,新建一个Single View APplication,然后在Storyboard中添加一个UIpicker View
,
2、实现UIPicker View的代理方法 --这里的数据是暂时的,还没添加数据
#pragma mark - UIPickerViewDelegate
/**
*返回每一列的数据个数
*/
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component==1)
{
return 2;
}else if (component==2)
{
return 3;
}else
{
return 4;
}
}
/**
*返回pickerView分几列,因为是省市区选择,所以分3列
*/
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
/**
*触发的事件
*/
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"ss");
}
/**
*通过自定义view去显示pickerView中的内容,这样做的好处是可以自定义的调整pickerView中显示内容的格式
*/
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *myView = nil;
myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
myView.textAlignment = NSTextAlignmentCenter;
myView.font = [UIFont systemFontOfSize:15]; //用label来设置字体大小
if (component==0) {
myView.text = @"1";
}else if (component==1)
{
myView.text = @"2";
}else
{
myView.text = @"3";
}
return myView;
}
3、接下来就是把刚才下载的那个area.plist文件中的数据加入到picker View中
①、首先定义三个NSArray。
NSArray *provinceArr,*cityArr,*areaArr;
②、先设置provinceArr.cityArr,areaArr.
provinceArr = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
cityArr = [[provinceArr objectAtIndex:0] objectForKey:@"cities"];
areaArr = [[cityArr objectAtIndex:0] objectForKey:@"areas"];
默认的province列先显示的第一列,此时刚好让city显示第一列的city,让area显示第一个city的area。
注:(此处这样设置是因为这个area.plist文件的结构的原因,你可以自己看一下这个plist文件)。
4、此时数据便有了,但是当滚动第一列时,第二列和第三列都没有变化,其实本质上就是在设置pickerView内容的代理方法中设置内容改变。重头戏来了。--修改上面的这个代理方法。
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component==0) { //当是省的时候
cityArr = [[provinceArr objectAtIndex:row] objectForKey:@"cities"];
[pickerView selectRow:0 inComponent:1 animated:NO];
[self.areaPicker reloadComponent:1];
if ([cityArr count]!=0) {
areaArr = [[cityArr objectAtIndex:0] objectForKey:@"areas"];
[pickerView selectRow:0 inComponent:2 animated:NO];
[self.areaPicker reloadComponent:2];
}
}
else if (component==1)
{
areaArr = [[cityArr objectAtIndex:row] objectForKey:@"areas"];
[pickerView selectRow:0 inComponent:2 animated:NO];
[self.areaPicker reloadComponent:2];
}
}
注:不要忘了重新设置每一列的数据个数
if (component==0) {
return [provinceArr count];
}else if(component==1)
{
return [cityArr count];
}else
{
return [areaArr count];
}
5、附加:
获取三列的内容
province= [[provinceArr objectAtIndex:[self.areaPicker selectedRowInComponent:0]] objectForKey:@"state"]; //获取province
city= [[cityArr objectAtIndex:[self.areaPicker selectedRowInComponent:1]] objectForKey:@"city"];
if ([areaArr count]!=0) {
area = [areaArr objectAtIndex:[self.areaPicker selectedRowInComponent:2]];
}
6、完成,附上源码。(故事版上的设置大家自己设置就行了)
//
// ViewController.m
// ProvinceCityDownTownChoose
//
// Created by xiaoguizi on 15/2/9.
// Copyright (c) 2015年 xiaoguizi. All rights reserved.
//
------.m文件------
#import "ViewController.h"
@interface ViewController ()
{
NSArray *provinceArr,*cityArr,*areaArr;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
provinceArr = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
cityArr = [[provinceArr objectAtIndex:0] objectForKey:@"cities"];
areaArr = [[cityArr objectAtIndex:0] objectForKey:@"areas"];
}
#pragma mark - UIPickerViewDelegate
/**
*返回每一列的数据个数
*/
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return [provinceArr count];
}else if(component==1)
{
return [cityArr count];
}else
{
return [areaArr count];
}
}
/**
*返回pickerView分几列,因为是省市区选择,所以分3列
*/
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
/**
*触发的事件
*/
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component==0) {
cityArr = [[provinceArr objectAtIndex:row] objectForKey:@"cities"];
[pickerView selectRow:0 inComponent:1 animated:NO];
[self.areaPicker reloadComponent:1];
if ([cityArr count]!=0) {
areaArr = [[cityArr objectAtIndex:0] objectForKey:@"areas"];
[pickerView selectRow:0 inComponent:2 animated:NO];
[self.areaPicker reloadComponent:2];
}
}
else if (component==1)
{
areaArr = [[cityArr objectAtIndex:row] objectForKey:@"areas"];
[pickerView selectRow:0 inComponent:2 animated:NO];
[self.areaPicker reloadComponent:2];
}
}
/**
*通过自定义view去显示pickerView中的内容,这样做的好处是可以自定义的调整pickerView中显示内容的格式
*/
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *myView = nil;
myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
myView.textAlignment = NSTextAlignmentCenter;
myView.font = [UIFont systemFontOfSize:15]; //用label来设置字体大小
if (component==0) {
myView.text =[[provinceArr objectAtIndex:row] objectForKey:@"state"];
}else if (component==1)
{
myView.text =[[cityArr objectAtIndex:row] objectForKey:@"city"];
}else
{
myView.text =[areaArr objectAtIndex:row];
}
return myView;
}
@end
------.h文件------
//
// ViewController.h
// ProvinceCityDownTownChoose
//
// Created by xiaoguizi on 15/2/9.
// Copyright (c) 2015年 xiaoguizi. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *areaPicker;
@end
时间: 2025-01-30 05:02:55