Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

ios - Show data in UIPickerView second component based on first component selection

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.

enter image description here

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];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...