根据第一个组件选择在UIPickerView第二个组件中显示数据 [英] Show data in UIPickerView second component based on first component selection

查看:98
本文介绍了根据第一个组件选择在UIPickerView第二个组件中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有两个组件的选择器。我希望如果我在所选组件的基础上在第一个组件中选择一行,它会显示相应数据的值。

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.

由于Picker显示英格兰在选择英格兰时拥有相应的俱乐部。我想为其他国家做同样的事情。但是我没有采用哪种方法。

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.

这是我的代码:

- (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];


推荐答案

您的代码......稍加修改..

your code.. with minor modifications..

didSelectRow

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

我有

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {

    IBOutlet UIPickerView *pickerView;
    NSMutableArray *arrayColors;
    NSMutableArray *Nations;
    NSMutableArray *England;
    NSMutableArray *Espana;
    NSMutableArray *Germany;
    NSString *club;
}

之后..你必须将de pickerView连接到你的ViewController(dataSource和delegate) )

after that.. you must connect de pickerView to yours ViewController (dataSource and delegate)

这篇关于根据第一个组件选择在UIPickerView第二个组件中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆