根据第一个组件select在UIPickerView第二个组件中显示数据

我正在使用两个组件的选取器。 我想如果我在选定的组件的基础上select第一个组件的一行,它显示相应的数据的值。

在这里输入图像说明

正如皮克在英格兰被选中时所显示的,英格兰有相应的俱乐部。 我想为其他国家做同样的事情。 但是我没有得到遵循的方法。

这是我的代码:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { return 2; } - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { if(component ==0) { return [Nations count]; } else{ if(country_flag==0) { return [England count]; } else if (country_flag==1) { return [Espana count]; } else if (country_flag==2) { return [Netherlands count]; } else if (country_flag==3) { return [Germany count]; } else if (country_flag==4) { return [Italy count]; } } return 0; } - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if(component ==0) { return [Nations objectAtIndex:row]; } else{ if(country_flag==0) { return [England objectAtIndex:row]; } else if (country_flag==1) { return [Espana objectAtIndex:row]; } else if (country_flag==2) { return [Netherlands objectAtIndex:row]; } else if (country_flag==3) { return [Germany objectAtIndex:row]; } else if (country_flag==4) { return [Italy objectAtIndex:row]; } } return 0; } - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { label.text=[Nations objectAtIndex:row]; str = [[NSString alloc]init]; if (country_flag==0) { str=@"England"; label_1.text=[NSString stringWithFormat:@"%@",str]; NSLog(@"%@",label_1); str=@""; country_flag =1; [picker reloadAllComponents]; } else if (country_flag==1) { str=@"Espana"; label_1.text=[NSString stringWithFormat:@"%@",str]; NSLog(@"%@",label_1); str=@""; country_flag =2; [picker reloadAllComponents]; } else if (country_flag==2) { str=@"Netherlands"; label_1.text=[NSString stringWithFormat:@"%@",str]; NSLog(@"%@",label_1); str=@""; country_flag =3; [picker reloadAllComponents]; } else if (country_flag==3) { str=@"Germany"; label_1.text=[NSString stringWithFormat:@"%@",str]; NSLog(@"%@",label_1); str=@""; country_flag =4; [picker reloadAllComponents]; } else if (country_flag==4) { str=@"Germany"; label_1.text=[NSString stringWithFormat:@"%@",str]; NSLog(@"%@",label_1); str=@""; // country_flag =4; [picker reloadAllComponents]; } } 

编辑:

这里是数据

 Nations = [[NSMutableArray alloc]initWithObjects:@"England",@"Espana",@"Germany",@"Netherlands",@"Germany",@"Italy", nil]; England=[[NSMutableArray alloc]initWithObjects:@"Arsenal",@"Chelsea",@"Manchester City",@"Manchester United",@"Liverpool",@"Tottenham",@"Fulham City",@"Stoke City",@"Sunderland",@"NewCastle United",@"Blackburn Rovers",@"Southampton",@"Wolvers",@"Aston Villa", nil]; Espana = [[NSMutableArray alloc]initWithObjects:@"Barcelona",@"Real Madrid",@"Valencia",@"Athletico Madrid",@"Athletico Balbao",@"Getafe CF",@"Sevilla CF", nil]; Netherlands = [[NSMutableArray alloc]initWithObjects:@"Celtics",@"Ajax",@"Amesterdam", nil]; Germany = [[NSMutableArray alloc]initWithObjects:@"Bayern Munich",@"Bermen",@"Fiorentina",@"Pampas",@"Nord", nil]; Italy = [[NSMutableArray alloc]initWithObjects:@"AC Milan",@"Inter Milan",@"Juventus", nil]; 

你的代码..稍作修改..

didSelectRow

 - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == 0) { club=[[NSString alloc] initWithFormat:@"%@" , [Nations objectAtIndex:row]]; [pickerView reloadComponent:1]; } 

}

之后…

 - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { if(component ==0) { return [Nations count]; } else { if ([club isEqualToString:@"Espana"]) { return [Espana count]; } if ([club isEqualToString:@"Germany"]) { return [Germany count]; } // if... else { return [England count]; } } return 0; } 

 - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if(component ==0) { return [Nations objectAtIndex:row]; } else { if ([club isEqualToString:@"Espana"]) { return [Espana objectAtIndex:row]; } if ([club isEqualToString:@"Germany"]) { return [Germany objectAtIndex:row]; } //if.... else { return [England objectAtIndex:row]; } } return 0; } 

UPD

在h文件中我有

 @interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> { IBOutlet UIPickerView *pickerView; NSMutableArray *arrayColors; NSMutableArray *Nations; NSMutableArray *England; NSMutableArray *Espana; NSMutableArray *Germany; NSString *club; } 

之后..你必须连接de pickerView到你的ViewController(数据源和委托) 在这里输入图像说明

通常你这样做。 你创build一个包含所有国家数据的plist文件,结果就像这样

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>England</key> <array> <string>Chelsea</string> <string>Arsenal</string> </array> <key>Spain</key> <array> <string>Barca</string> <string>Real</string> </array> </dict> </plist> 

假设在属性或某处定义了以下内容

 NSDictionary *countryClubs; NSArray *countries; NSArray *clubs; 

你然后做那样的事情

 - (void)viewDidLoad { [super viewDidLoad]; NSBundle *bundle = [NSBundle mainBundle]; NSURL *plistFile = [bundle URLForResource:@"myPListFile" withExtension:@"plist"]; NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistFile]; self.countryClubs = dictionary; NSString *selectedCountry = [self.countries objectAtIndex:0]; NSArray *array = [countryClubs objectForKey:selectedCountry]; self.clubs = array; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == 0) return [self.countries count]; return [self.clubs count]; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == 0) return [self.countries objectAtIndex:row]; return [self.clubs objectAtIndex:row]; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == 0) { NSString *selectedCountry = [self.countries objectAtIndex:row]; NSArray *array = [countryClubs objectForKey:selectedCountry]; self.clubs = array; [picker selectRow:0 inComponent:1 animated:YES]; [picker reloadComponent:1]; } } 

这应该对你有所帮助 我希望没有太多的错字错误,但至less应该有一个总的想法。

我认为问题出在你的didSelectRow方法中。 在允许select新的国家之前,您检查哪个国家已经被选中(在country_flag )。 我不认为有这样的理由。

允许任何国家被选中。 然后根据所选的行设置country_flag

只要做country_flag = row+1;

如果这一点很重要,那么你可以通过if语句来设置你的标签。

看看这个,我认为它可以帮助你

  1. 首先确保你已经连接你的select器和IBOutlet。

  2. 如果你遇到问题,你可以使用

[thePickerView reloadComponent:(NSInteger)component];

代替

 [picker reloadComponent:(NSInteger)component];