- (void)viewDidLoad
{
[super viewDidLoad];
// 1.创建pickerview
// pickerview有默认的frame
UIPickerView \*pickerView = \[\[UIPickerView alloc\] init\];
pickerView.dataSource = self;
pickerView.delegate = self;
\[self.view addSubview:pickerView\];
self.pickerView = pickerView;
}
#pragma mark - UIPickerViewDataSource
// 告诉系统有多少列
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ;
}
// 告诉系统有多少行
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if ( == component) {
// 省份列
return self.provinces.count;
}else
{
// 城市列
// 获取第0列选中的行
NSInteger selectIndex = [self.pickerView selectedRowInComponent:];
// 1.根据第0列选中的行数获取对应的省
NJProvince *province = self.provinces[selectIndex];
// 2.获取对应省份对应的城市
NSArray *cities = province.cities;
// 3.返回城市的个数r
return cities.count;
}
}
#pragma mark - UIPickerViewDelegate
// 告诉系统每一行显示什么内容
(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(@"titleForRow");
if ( == component) {
// 省份
// 1.获取对应行对应的省份模型
NJProvince *province = self.provinces[row];
// 2.返回省份的名称
return province.name;
}else
{
// 城市列
// 0.获取第0列选中的行数
NSInteger selectIndex = [pickerView selectedRowInComponent:];
// 1.获取对应的省份
NJProvince *province = self.provinces[selectIndex];
// 2.获取对应的城市
return province.cities[row];
}
}
// 监听pickerView的选中
手机扫一扫
移动阅读更方便
你可能感兴趣的文章