//在picker view的练习国旗的选择中
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSLog(@"%p",view);
ContryView *view1=(ContryView *)view;
//该view的重用在iOS7有bug
if (!view1) {
view1=[ContryView getCountryView];
}
view1.country=self.contrys[row];
return view1;
}
可以自定义一个view类利用xib生产view ,设置一个model数据源后返回一个view给picker view即可.
//该方法可以设置picker view的每列的每行的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 44;
}
在省份选择的练习中,第二行的城市是根据第一行的状态选择的,先确定第一行的选择是什么,然后加载第二行的数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
Shengfen *shenfen=self.shengfen[component];
if (component==0) {
return self.shengfen.count;
}else{
NSInteger select=[pickerView selectedRowInComponent:0];
Shengfen *shenfen1=self.shengfen[select];
return shenfen1.cities.count;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component==0) {
Shengfen *shenfen=self.shengfen[row];
return shenfen.name;
}else{
NSInteger select=[pickerView selectedRowInComponent:0];
Shengfen *shenfen=self.shengfen[select];
return shenfen.cities[row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component==0) {
[pickerView reloadComponent:1];
//第一列选择之后,立刻跳到第二列的第一行
[pickerView selectRow:0 inComponent:1 animated:YES];
}
NSInteger select=[pickerView selectedRowInComponent:0];
Shengfen *shenfen=self.shengfen[select];
NSString *str=nil;
if (component==1) {
str= shenfen.cities[row];
}
NSString *str1=[NSString stringWithFormat:@"%@ %@",shenfen.name,str];
self.label.text=str1;
}