如何根据UIPickerview中的第一个组件选择在第二个组件中设置数据

时间:2014-06-27 14:00:36

标签: ios iphone objective-c uipickerview

我正在使用带有三个组件的UIpicker。我希望如果我在所选组件的基础上在第一个组件中选择一行,它会显示第二个组件中相应数据的值。 我使用数组作为第一个组件值,如 @" UK",@" US",@" EU" 。如果用户我想要在第一个组件中选择英国然后我想加载UKsize1 = [NSArray arrayWithObjects:@" 1",@" 2",@" 3",@&#34 ; 4",@" 5",nil];在第二个组件中,如果用户在第一个组件中选择了US,那么我希望在第二个组件中加载EUsize2=[NSArray arrayWithObjects:@"11",@"22",@"33",@"44",@"55", nil];。与欧盟价值相同,我想加载USsize3=[NSArray arrayWithObjects:@"111",@"222",@"333",@"444",@"555", nil];。我想要根据第一个组件改变第二个组件值,第三个组件值是固定的。

这个代码我试过但我没有得到确切的价值。

- (id)init 
{
    if (self = [super init]) {
        notesToDisplayForKey = [NSArray arrayWithObjects: @"UK", @"US", @"EU", nil];
        scaleNames = [NSArray arrayWithObjects: @"11", @"22", @"33", @"555",@"666",@"777",nil];
        scaleValue=[NSArray arrayWithObjects: @"1" ,@"2", @"3", @"4",@"5",@"6", @"7",@"8",@"9", @"10",@"11",nil];
        UKsize1=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
        EUsize2=[NSArray arrayWithObjects:@"11",@"22",@"33",@"44",@"55", nil];
        USsize3=[NSArray arrayWithObjects:@"111",@"222",@"333",@"444",@"555", nil];
    }
    return self;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{ 
    // Returns
    switch (component) {
        case 0: return [notesToDisplayForKey count];
        case 1:
            if([selectedKey isEqualToString:@"UK" ])
            {

                return [UKsize1 count];
            }
            else if([selectedKey isEqualToString:@"US"] )
            {

                return [USsize3 count];
            }
            else
            {

                return [EUsize2 count];
            }
            //return [scaleNames count];
        case 2: return [scaleValue count];
        default:break;
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0: return [notesToDisplayForKey objectAtIndex:row];
        case 1:if([selectedKey isEqualToString:@"UK" ])
        {
            selectedScale = [scaleNames objectAtIndex:row];
            return selectedScale;
        }
        else if([selectedKey isEqualToString:@"US"] )
        {
            selectedScale = [USsize3 objectAtIndex:row];
            return selectedScale;
        }
        else
        {
            selectedScale = [EUsize2 objectAtIndex:row];
            return selectedScale;
        }
        case 2: return [scaleValue objectAtIndex:row];
        default:break;
    }
    return nil;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
  //  NSLog(@"Row %i selected in component %i", row, component);
    switch (component) {
        case 0:
            selectedKey = [notesToDisplayForKey objectAtIndex:row];
            return;

        case 1:

           // selectedScale = [scaleNames objectAtIndex:row];
            if([selectedKey isEqualToString:@"UK" ])
                  {
                      selectedScale = [scaleNames objectAtIndex:row];
                      return;
                  }
                else if([selectedKey isEqualToString:@"US"] )
                 {
                 selectedScale = [USsize3 objectAtIndex:row];
                return;
                 }
                else
                {
                 selectedScale = [EUsize2 objectAtIndex:row];
                 return;
                }
            return;

        case 2:
            selectedValue = [scaleValue objectAtIndex:row];
            return;
        default:break;
    }
}

screenshot

2 个答案:

答案 0 :(得分:3)

好像你没有重新加载选择器....每次你需要更改选择器中的数据时,你需要像UITableView一样重新加载选择器。

//重新加载整个视图或单个组件

- (void)reloadAllComponents;
- (void)reloadComponent:(NSInteger)component;

答案 1 :(得分:3)

只需检查此方法即可。

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
      //  NSLog(@"Row %i selected in component %i", row, component);
        switch (component) {
            case 0:
                selectedKey = [notesToDisplayForKey objectAtIndex:row];
                  [pickerView reloadAllComponents];
                return;

            case 1:

               // selectedScale = [scaleNames objectAtIndex:row];
                if([selectedKey isEqualToString:@"UK" ])
                      {
                          selectedScale = [UKsize1 objectAtIndex:row];
                          return;
                      }
                    else if([selectedKey isEqualToString:@"US"] )
                     {
                     selectedScale = [USsize3 objectAtIndex:row];
                    return;
                     }
                    else
                    {
                     selectedScale = [EUsize2 objectAtIndex:row];
                     return;
                    }
                return;

            case 2:
                selectedValue = [scaleValue objectAtIndex:row];
                return;
            default:break;
        }
    }