#import "ViewController.h"
@interface ViewController ()
<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)UIPickerView *pickerView;
@property(nonatomic,strong)NSArray *rowDataSource;
@property(nonatomic,strong)NSArray *row2DataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.rowDataSource=@[@"安徽省",@"浙江省",@"山东省",@"江苏省",@"陕西省",@"福建省",@"湖北省",@"湖南省"];
self.row2DataSource=@[@"芜湖市",@"合肥市",@"南京市",@"杭州市",@"广东市",@"亳州市",@"自贡市",@"长沙市"];
self.pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,400)];
//UIPickerView 选择的数据必须提供,可以有多列数据, 每一列有多行数据
self.pickerView.dataSource=self;
self.pickerView.delegate=self;
[self.viewaddSubview:self.pickerView];
}
//列数
-(NSInteger)numberOfComponentsInPickerView:(nonnullUIPickerView*)pickerView
{
return 2;
}
//每一列的行数
-(NSInteger)pickerView:(nonnullUIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
NSIntegercount =0;
if(component ==0)
{
return self.rowDataSource.count;
}
elseif(component ==1)
{
count =self.row2DataSource.count;
}
returncount;
}
//每一行的数据
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString* title =@"";
if(component ==0)
{
title=self.rowDataSource[row];
}
elseif(component ==1)
{
title =self.row2DataSource[row];
}
returntitle;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end