示例圖片
//// 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 之前 默認(rèn)高度是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 數(shù)據(jù)源
// 返回的列數(shù)
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// 返回的行數(shù)
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return array1.count;
}else{
return array2.count;
}
}
#pragma mark UIPickerViewDelegate 委托
// 返回行的內(nèi)容(自定義) 也可以返回圖片內(nèi)容
-(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 ;
}
}
// 返回每行的內(nèi)容
-(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 之前 默認(rèn)高度是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 數(shù)據(jù)源
// 返回的列數(shù)
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// 返回的行數(shù)
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return array1.count;
}else{
// 第一列選中的行數(shù)
NSInteger firstSelectRow = [pickerView selectedRowInComponent:0];
//獲取省的數(shù)據(jù)
NSDictionary *dic =array1[firstSelectRow];
//獲取城市的數(shù)據(jù)
NSArray *cityArray = [dic objectForKey:@"areas"];
return cityArray.count;
}
}
#pragma mark UIPickerViewDelegate 委托
//點(diǎn)擊事件
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// 第一列選中事件
if (component == 0) {
// reloadAllComponent 刷新所有 列數(shù)
// 刷新指定的列數(shù)的數(shù)據(jù) 【點(diǎn)擊左邊 刷新右邊 】
[pickerView reloadComponent:1];
// 【讓第二列滑到多行】【然后滑動(dòng)第一列到別行】【讓第二列回到第一行第一個(gè)位置】
[pickerView selectRow:0 inComponent:1 animated:YES];
}
}
// 返回行的內(nèi)容(自定義)
//-(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 ;
//
//
// }
//}
// 返回每行的內(nèi)容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
NSDictionary *dic = [array1 objectAtIndex:row];
return [dic objectForKey:@"state"];
}else{
// 第一列選中的行數(shù)
NSInteger firstSelectRow = [pickerView selectedRowInComponent:0];
//獲取省的數(shù)據(jù)
NSDictionary *dic =array1[firstSelectRow];
//獲取城市的數(shù)據(jù)
NSArray *cityArray = [dic objectForKey:@"areas"];
// 防止兩個(gè)快速滑動(dòng)時(shí)候,數(shù)組越界而崩潰的問題
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