//// RootViewController.m// UIPickerView//// Created by 林杰 on 16/8/2.// Copyright © 2016年 TenMios. All rights reserved.//#import "RootViewController.h"@interface RootViewController ()@end
#define SWidth [UIScreen mainScreen].bounds.size.width
#define SHeight [UIScreen mainScreen].bounds.size.height
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//iphone 5 之前 默认高度是216
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 100, SWidth, 100) ];
pickerView.backgroundColor = [UIColor grayColor];
pickerView.delegate = self;
pickerView.dataSource = self;
[self.view addSubview: pickerView];
array1 = @[@"福建省",@"广东省",@"山东省",@"湖南省",@"湖北省"];
array2 = @[@"福州",@"广东",@"山东",@"湖南",@"湖北"];
}
#pragma mark UIPickerViewDataSource 数据源
// 返回的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// 返回的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return array1.count;
}else{
return array2.count;
}
}
#pragma mark UIPickerViewDelegate 委托
// 返回行的内容(自定义) 也可以返回图片内容
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
if(component == 0)
{
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor redColor];
return view ;
}else{
UIView *view2 = [[UIView alloc]init];
view2.backgroundColor = [UIColor blueColor];
return view2 ;
}
}
// 返回每行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
return array1[row];
}else{
return array2[row];
}
}
//列的高度 (左大右小)
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == 0) {
return 200;
}else{
return 100;
}
}
//行的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
if (component == 0) {
return 50;
}else{
return 50;
}
}
@end
//************************************************** 例子一 //// RootViewController.m// UIPickerView//// Created by 林杰 on 16/8/2.// Copyright © 2016年 TenMios. All rights reserved.//#import "RootViewController.h"@interface RootViewController ()@end
#define SWidth [UIScreen mainScreen].bounds.size.width
#define SHeight [UIScreen mainScreen].bounds.size.height
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//iphone 5 之前 默认高度是216
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 100, SWidth, 300) ];
pickerView.backgroundColor = [UIColor grayColor];
pickerView.delegate = self;
pickerView.dataSource = self;
[self.view addSubview: pickerView];
// array1 = @[@"福建省",@"广东省",@"山东省",@"湖南省",@"湖北省"];
// array2 = @[@"福州",@"广东",@"山东",@"湖南",@"湖北"];
NSString *path = [[NSBundle mainBundle ] pathForResource:@"area" ofType:@"plist"];
array1 = [NSArray arrayWithContentsOfFile:path];
NSLog(@"%@",array1);
}
#pragma mark UIPickerViewDataSource 数据源
// 返回的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// 返回的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return array1.count;
}else{
// 第一列选中的行数
NSInteger firstSelectRow = [pickerView selectedRowInComponent:0];
//获取省的数据
NSDictionary *dic =array1[firstSelectRow];
//获取城市的数据
NSArray *cityArray = [dic objectForKey:@"areas"];
return cityArray.count;
}
}
#pragma mark UIPickerViewDelegate 委托
//点击事件
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// 第一列选中事件
if (component == 0) {
// reloadAllComponent 刷新所有 列数
// 刷新指定的列数的数据 【点击左边 刷新右边 】
[pickerView reloadComponent:1];
// 【让第二列滑到多行】【然后滑动第一列到别行】【让第二列回到第一行第一个位置】
[pickerView selectRow:0 inComponent:1 animated:YES];
}
}
// 返回行的内容(自定义)
//-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
//{
//
// if(component == 0)
//{
// UIView *view = [[UIView alloc]init];
// view.backgroundColor = [UIColor redColor];
// return view ;
//
// }else{
// UIView *view2 = [[UIView alloc]init];
// view2.backgroundColor = [UIColor blueColor];
// return view2 ;
//
//
// }
//}
// 返回每行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
NSDictionary *dic = [array1 objectAtIndex:row];
return [dic objectForKey:@"state"];
}else{
// 第一列选中的行数
NSInteger firstSelectRow = [pickerView selectedRowInComponent:0];
//获取省的数据
NSDictionary *dic =array1[firstSelectRow];
//获取城市的数据
NSArray *cityArray = [dic objectForKey:@"areas"];
// 防止两个快速滑动时候,数组越界而崩溃的问题
if(row < cityArray.count)
{
return [cityArray objectAtIndex:row];
}else
{
return @"";
}
}
}
//列的高度 (左大右小)
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == 0) {
return 200;
}else{
return 100;
}
}
//行的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
if (component == 0) {
return 50;
}else{
return 50;
}
}
//
//
@end
收集iOS--UIPickerView的常用属性
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...