UIPickerView 实现城市选择三级联动。
需要用到一些宏定义
#define InitObject(vc) [[vc alloc]init]
#define RECT(x,y,width,height) CGRectMake(x,y,width,height)
上代码
初始化话一个UIPickerView
InitObject(UIPickerView); 是一个宏定义
- (void)createPicker
{
self.picker = InitObject(UIPickerView);
self.picker.frame = RECT(0,30*HeightProportion, Main_Screen_Width, 150*HeightProportion);
self.picker.dataSource = self;
self.picker.delegate = self;
self.picker.showsSelectionIndicator = YES;
self.picker.backgroundColor = [UIColor whiteColor];
[self.pickerBackView addSubview:self.picker];
[self.backView addSubview:self.pickerBackView];
//加载城市plist
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];
cityArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
//刷新第一列数据
[self.picker reloadComponent:0];
//刷新第二列
[self.picker reloadComponent:1];
//刷新第3列 当第一列是省级城市的时候需要刷新
//[self.picker reloadComponent:3];
}
实现代理方法
#pragma mark-----------pickerView delegate-------------------------
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
//一共有几列 这里是3级联动
return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//第1列有多少item
if (0 == component)
{
return cityArray.count;
}
//第2列有多少item
if (1 == component) {
//获取第2列选中项 获取对应字典拿到下级数组
NSInteger rowProvince = [pickerView selectedRowInComponent:0];
NSDictionary * province = [cityArray objectAtIndex:rowProvince];
NSArray * city = [province objectForKey:@"cities"];
return city.count;
}else{
//获取第3列选中项 获取对应字典拿到下级数组
NSInteger rowProvince = [pickerView selectedRowInComponent:0];
NSDictionary * province = [cityArray objectAtIndex:rowProvince];
NSArray * city = [province objectForKey:@"cities"];
NSInteger rowCity = [pickerView selectedRowInComponent:1];
NSDictionary * district = [city objectAtIndex:rowCity];
NSArray *country = [district objectForKey:@"areas"];
return country.count;
}
}
//title 显现 类似cell的 (
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
) 方法
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (0 == component) {
return [[cityArray objectAtIndex:row] objectForKey:@"state"];
}
if(1 == component){
NSInteger rowProvince = [pickerView selectedRowInComponent:0];
NSDictionary * province = [cityArray objectAtIndex:rowProvince];
NSArray * city = [province objectForKey:@"cities"];
return [[city objectAtIndex:row] objectForKey:@"city"];
}else{
NSInteger rowProvince = [pickerView selectedRowInComponent:0];
NSDictionary * province = [cityArray objectAtIndex:rowProvince];
NSArray * city = [province objectForKey:@"cities"];
NSInteger rowCity = [pickerView selectedRowInComponent:1];
NSDictionary * district = [city objectAtIndex:rowCity];
NSArray *country = [district objectForKey:@"areas"];
return [country objectAtIndex:row];
}
}
滚动处理
#pragma mark - 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(0 == component){
[pickerView reloadComponent:1];
[pickerView selectRow:0 inComponent:1 animated:YES];
[pickerView reloadComponent:2];
[pickerView selectRow:0 inComponent:2 animated:YES];
}
if(1 == component)
{
[pickerView reloadComponent:2];
[pickerView selectRow:0 inComponent:2 animated:YES];
}
//在其他界面需要拿到数据 可以无视
//NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];
//NSMutableArray * cityArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
//获取选中的内容
NSString * one,*two,*three;
NSInteger rowProvince = [self.picker selectedRowInComponent:0];
NSDictionary * province = [cityArray objectAtIndex:rowProvince];
NSArray * city = [province objectForKey:@"cities"];
NSInteger rowCity = [self.picker selectedRowInComponent:1];
NSDictionary * district = [city objectAtIndex:rowCity];
NSArray *country = [district objectForKey:@"areas"];
NSInteger rowMin = [self.picker selectedRowInComponent:2];
one = [province objectForKey:@"state"];
if (city.count>0) {
two = [district objectForKey:@"city"];
}
if (country.count>0) {
three = [country objectAtIndex:rowMin];
}
address = [NSString stringWithFormat:@"%@%@%@",one,two,three];
}
以上就是基本实现方法了,可以考虑把这个封装在一个BaseViewController 将UIPickerView做成属性 在子控制器直接调用。
-----city.plist下载地址
网友评论