问题
I am using a picker with two components. I want if I select a row in first component on the basis of selected component it shows the value of the corresponding data.

As Picker is showing that England has corresponding clubs when England is selected. I want to do same for the other countries. But I am not getting which approach to follow.
Here is my code:
- (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];
}
}
EDIT:
Here is the data
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];
回答1:
your code.. with minor modifications..
didSelectRow
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
club=[[NSString alloc] initWithFormat:@"%@" , [Nations objectAtIndex:row]];
[pickerView reloadComponent:1];
}
}
after that...
- (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;
}
and
- (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
in h file I have
@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *arrayColors;
NSMutableArray *Nations;
NSMutableArray *England;
NSMutableArray *Espana;
NSMutableArray *Germany;
NSString *club;
}
after that.. you must connect de pickerView to yours ViewController (dataSource and delegate)

回答2:
Usually you do it like that. You create a plist file containing the data of all your countries the result is something like
<?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>
assuming the following are defined in the properties or somewhere
NSDictionary *countryClubs;
NSArray *countries;
NSArray *clubs;
you then do things like that
- (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];
}
}
This should help you. I hope there is not to much typo mistakes but you should at least get the general idea.
回答3:
I think the problem is in your didSelectRow
method. You check which country is already selected (in country_flag
) before allowing the selection of a new country. I don't think there's any reason to do this.
Allow any country to be selected. Then set country_flag
based on the row that was selected.
Just do country_flag = row+1;
You can then have your if
statements to set up your label if that's important.
回答4:
Look at this, i think it can help you
First of all make sure that you have connected your picker with IBOutlet .
Still if you get the issue you can use
[thePickerView reloadComponent:(NSInteger)component];
instead of
[picker reloadComponent:(NSInteger)component];
来源:https://stackoverflow.com/questions/13493402/show-data-in-uipickerview-second-component-based-on-first-component-selection