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

时间:2012-11-21 12:34:22

标签: ios uipickerview

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

enter image description here

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

这是我的代码:

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

4 个答案:

答案 0 :(得分:8)

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

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(dataSource和delegate) enter image description here

答案 1 :(得分:0)

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

  1. 首先确保您已将选择器连接到 IBOutlet。

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

  3. [thePickerView reloadComponent:(NSInteger)component];

    而不是

    [picker reloadComponent:(NSInteger)component]; 
    

答案 2 :(得分:0)

通常你这样做。 您创建一个包含所有国家/地区数据的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];
    }
}

这应该对你有所帮助。我希望没有太多错字错误,但你至少应该得到一般的想法。

答案 3 :(得分:0)

我认为问题出在您的didSelectRow方法中。在允许选择新国家之前,您可以检查已选择的国家/地区(country_flag)。我认为没有任何理由这样做。

允许选择任何国家/地区。然后根据所选行设置country_flag

只需country_flag = row+1;

如果这很重要,您可以使用if语句设置标签。