UIPickerView(省动联动 两列有关联)
"【案例: 省市联动】"
》添加Province模型加载plist文件
》实现PickerView的数据源和代理(用Main.storyboard)
(1)返回两组数据
(2)实现PickerView的代理,使用【-(UIView *)pickerView:viewForRow:forComponent:reusingView:】方法,返回一个Label
(3)添加一个当前省份选中索引IndexOfProv
(4)默认城市那一组数据 返回 第一个省份的城市数据
(5)当前省份选中的索引改变时,刷新城市数据并默认选中的第一个城市
*[pickerView reloadComponent:1];
*[pickerView selectRow:0 inComponent:1 animated:YES];
》掌握设置组宽的代理方法
(1)实现【-(CGFloat)pickerView:widthForComponent:】方法设置组的宽度
(2)设置两列View的背景颜色不同看出宽度的区别
Province.h
//
// Province.h
// 03.省市联动
//
// Created by Yong Feng Guo on 14-12-19.
// Copyright (c) 2014年 Fung. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Province : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,strong)NSArray *cities;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)provinceWithDict:(NSDictionary *)dict;
+(NSArray *)provinceList;
@end
Province.m
//
// Province.m
// 03.省市联动
//
// Created by Yong Feng Guo on 14-12-19.
// Copyright (c) 2014年 Fung. All rights reserved.
//
#import "Province.h"
@implementation Province
-(instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)provinceWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
+(NSArray *)provinceList
{
//plist 文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil];
NSArray *provincePlist = [NSArray arrayWithContentsOfFile:filePath];
NSMutableArray *provinceM = [NSMutableArray array];
for (NSDictionary *dic in provincePlist) {
Province *prov = [Province provinceWithDict:dic];
[provinceM addObject:prov];
}
return provinceM;
}
@end
ViewController.m
//
// ViewController.m
// 03.省市联动
//
// Created by huan on 16/1/8.
// Copyright © 2016年 huanxi. All rights reserved.
//
#import "ViewController.h"
#import "Province.h"
@interface ViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) NSArray *provinces;
@property (nonatomic, assign) NSInteger indexOfProvince;//当前默认选中的省份
@end
@implementation ViewController
-(NSArray *)provinces
{
if (!_provinces) {
_provinces = [Province provinceList];
}
return _provinces;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"%@", self.provinces);
}
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
// return 12;
if (component == 0) {
return self.provinces.count;
}
//获得对应省份的城市的个数
Province *province = self.provinces[self.indexOfProvince];
return province.cities.count;
}
#pragma mark 显示数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//对应列行的省份
Province *province = self.provinces[row];
if (component == 0) {//显示省份的名字
return province.name;
}
//获取选中的城市,显示城市的名字
Province *selectedProvice = self.provinces[self.indexOfProvince];
return selectedProvice.cities[row];
}
//-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
// UILabel *label = (UILabel *)view;
// if (!label) {
// label = [[UILabel alloc] init];
// }
// if (component == 0) {//显示省份的名字
//
// Province *province = self.provinces[row];
// label.text = province.name;
// label.backgroundColor = [UIColor grayColor];
// }
// else{
// //获取选中的城市,显示城市的名字
// Province *selectedProvice = self.provinces[self.indexOfProvince];
// label.text = selectedProvice.cities[row];
// label.backgroundColor = [UIColor blueColor];
// }
// return label;
//
//}
#pragma mark 选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {//第一列省的选中改变后,就更新第二列数据
//更新选中省份的索引
self.indexOfProvince = row;
//刷新数据
//全部刷新
// [pickerView reloadAllComponents];
//部分刷新
[pickerView reloadComponent:1];
//不管之前第二列选中第几行,重新刷新数据后,都显示第二列的第一行
[pickerView selectRow:0 inComponent:1 animated:YES];
}
}
#pragma mark 设置宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
if (component == 0) {
return 80;
}
return 200;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end