3

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

在此处输入图像描述

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

这是我的代码:

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

4 回答 4

8

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

选择行

- (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 (数据源和委托) 在此处输入图像描述

于 2012-11-21T14:19:37.890 回答
0

看看这个,我想它可以帮助你

  1. 首先确保您已将您的选择器与 IBOutlet 连接起来。

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

[thePickerView reloadComponent:(NSInteger)component];

代替

[picker reloadComponent:(NSInteger)component]; 
于 2012-11-21T13:10:15.253 回答
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];
    }
}

这应该可以帮助你。我希望没有太多的拼写错误,但您至少应该了解总体思路。

于 2012-11-21T13:37:24.743 回答
0

我认为问题出在你的didSelectRow方法上。country_flag在允许选择一个新国家之前,您检查已经选择了哪个国家(在 中)。我认为没有任何理由这样做。

允许选择任何国家。然后country_flag根据选择的行进行设置。

做就是了country_flag = row+1;

然后,如果这很重要,您可以让您的if声明来设置您的标签。

于 2012-11-21T13:45:14.240 回答