UIPickerView的属性和常用方法 举例:显示省份和城市

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 
  5 @property(strong, nonatomic) UIPickerView * pickerView;
  6 @property(strong, nonatomic) UILabel * label;
  7 @property(strong, nonatomic) UIButton * button;
  8 @property (nonatomic, strong)  NSDictionary  *pickerData; //保存全部数据
  9 @property (nonatomic, strong)  NSArray  *pickerProvincesData;//当前的省数据
 10 @property (nonatomic, strong)  NSArray  *pickerCitiesData;//当前的省下面的市数据
 11 
 12 @end
 13 
 14 @implementation ViewController
 15 
 16 - (void)viewDidLoad {
 17     [super viewDidLoad];
 18     // Do any additional setup after loading the view, typically from a nib.
 19     
 20     self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 48, 320, 162)];
 21     self.pickerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
 22     [self.view addSubview:self.pickerView];
 23     self.label = [[UILabel alloc]initWithFrame:CGRectMake(63, 285, 195, 33)];
 24     self.label.textAlignment = NSTextAlignmentCenter;
 25     self.label.text = @"显示信息";
 26     self.label.backgroundColor = [UIColor groupTableViewBackgroundColor];
 27     [self.view addSubview:self.label];
 28     self.button = [[UIButton alloc]initWithFrame:CGRectMake(88, 413, 144, 44)];
 29     [self.button setTitle:@"显示在Label上" forState:UIControlStateNormal];
 30     self.button.backgroundColor = [UIColor brownColor];
 31     [self.button addTarget:self action:@selector(tapShow) forControlEvents:UIControlEventTouchUpInside];
 32     [self.view addSubview:self.button];
 33     
 34     NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PC" ofType:@"plist"];
 35     //获取属性列表文件中的全部数据
 36     NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
 37     self.pickerData = dict;
 38     
 39     //省份名数据
 40     self.pickerProvincesData =  [self.pickerData allKeys];
 41     
 42     //默认取出第一个省的所有市的数据
 43     NSString *seletedProvince = [self.pickerProvincesData objectAtIndex:0];
 44     self.pickerCitiesData = [self.pickerData objectForKey:seletedProvince];
 45     
 46     self.pickerView.dataSource = self;
 47     self.pickerView.delegate = self;
 48     
 49     
 50 }
 51 
 52 - (void)tapShow
 53 {
 54     NSInteger row1 = [self.pickerView selectedRowInComponent:0];
 55     NSInteger row2 = [self.pickerView selectedRowInComponent:1];
 56     //NSLog(@"row1 = %lu",row1);
 57     //NSLog(@"row2 = %lu",row2);
 58     NSString *selected1 = [self.pickerProvincesData objectAtIndex:row1];
 59     NSString *selected2 = [self.pickerCitiesData objectAtIndex:row2];
 60     
 61     NSString *title = [[NSString alloc] initWithFormat:@"%@,%@",
 62                        selected1,selected2];
 63     
 64     self.label.text = title;
 65     
 66 }
 67 
 68 #pragma mark 实现协议UIPickerViewDataSource方法
 69 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
 70     return 2;
 71 }
 72 //初始化时component先为0,后为1,返回省份个数和和市的个数,以确定省份的行数和市的临时初始化行数。此后选择省的时候component为1,返回市的个数
 73 - (NSInteger)pickerView:(UIPickerView *)pickerView
 74 numberOfRowsInComponent:(NSInteger)component {
 75     if (component == 0)
 76     {//省份个数
 77         return [self.pickerProvincesData count];
 78     } else
 79     {//市的个数
 80         return [self.pickerCitiesData count];
 81     }
 82     
 83 }
 84 
 85 #pragma mark 实现协议UIPickerViewDelegate方法
 86 //选择省份的时候component先为0,后为1。然后选择市的时候返回市所在的row号。将其省和市显示在pickerview的显示条上
 87 -(NSString *)pickerView:(UIPickerView *)pickerView
 88             titleForRow:(NSInteger)row forComponent:(NSInteger)component {
 89     //NSLog(@"row2 = %lu",row);
 90     //NSLog(@"component2 = %lu",component);
 91     if (component == 0)
 92     {//选择省份名
 93         return [self.pickerProvincesData objectAtIndex:row];
 94     } else
 95     {//选择市名
 96         return [self.pickerCitiesData objectAtIndex:row];
 97     }
 98 }
 99 //只有选择省份的时候component为0,返回省所在的row号,然后根据省份取出省所有的市。选择市的时候component为1。(作用为重新加载数据到pickerview上)
100 - (void)pickerView:(UIPickerView *)pickerView
101       didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
102     //NSLog(@"row3 = %lu",row);
103     //NSLog(@"component3 = %lu",component);
104     if (component == 0)
105     {
106         NSString *seletedProvince = [self.pickerProvincesData objectAtIndex:row];
107         NSArray *array = [self.pickerData objectForKey:seletedProvince];
108         //NSLog(@"array = %@",array);
109         self.pickerCitiesData = array;//将所选省的所有市存入pickerCitiesData
110         [self.pickerView reloadComponent:1];
111     }
112 }
113 - (void)didReceiveMemoryWarning {
114     [super didReceiveMemoryWarning];
115     // Dispose of any resources that can be recreated.
116 }
117 
118 @end
View Code

1、常用属性

//显示选中框

showsSelectionIndicator

2、常用方法

//返回rows的所有行号行号

- (NSInteger)numberOfRowsInComponent:(NSInteger)component;

//重新加载所有的元素

- (void)reloadAllComponents;

//通过赋值加载所选择的数据

- (void)reloadComponent:(NSInteger)component;

//协议UIPickerViewDataSource方法

//确定轮子的列数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

return 2;

}

//初始化时component先为0,后为1,返回省份个数和和市的个数,以确定省份的行数和市的临时初始化行数。此后选择省的时候component为1,返回市的个数

- (NSInteger)pickerView:(UIPickerView *)pickerView

numberOfRowsInComponent:(NSInteger)component {

if (component == 0)

{//省份个数

return [self.pickerProvincesData count];

} else

{//市的个数

return [self.pickerCitiesData count];

}

 

}

//协议UIPickerViewDelegate方法

//选择省份的时候component先为0,后为1。然后选择市的时候返回市所在的row号。将其省和市显示在pickerview的显示条上

-(NSString *)pickerView:(UIPickerView *)pickerView

titleForRow:(NSInteger)row forComponent:(NSInteger)component {

//NSLog(@"row2 = %lu",row);

//NSLog(@"component2 = %lu",component);

if (component == 0)

{//选择省份名

return [self.pickerProvincesData objectAtIndex:row];

} else

{//选择市名

return [self.pickerCitiesData objectAtIndex:row];

}

}

 

//只有选择省份的时候component为0,返回省所在的row号,然后根据省份取出省所有的市。选择市的时候component为1。(作用为重新加载数据到pickerview上)

- (void)pickerView:(UIPickerView *)pickerView

  didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

//NSLog(@"row3 = %lu",row);

//NSLog(@"component3 = %lu",component);

if (component == 0)

{

NSString *seletedProvince = [self.pickerProvincesData objectAtIndex:row];

NSArray *array = [self.pickerData objectForKey:seletedProvince];

//NSLog(@"array = %@",array);

self.pickerCitiesData = array;//将所选省的所有市存入pickerCitiesData

[self.pickerView reloadComponent:1];

}

}

 

posted @ 2015-08-27 20:56  大胖子2015  阅读(272)  评论(0编辑  收藏  举报