如何在第二组分基于在UIPickerview第一组分选择设置数据(how to set the da

2019-10-20 08:21发布

我使用的是UIpicker由三个部分组成。 我想,如果我选择所选组件的基础上,在第一成分的排它示出了在第二组分中的相应的数据的值。 我用阵列,用于像@“UK”,@“US”第一分量值,@“EU”。现在我想如果用户在第一组分选择UK然后我想加载UKsize1 = [NSArray的arrayWithObjects:@“1”,@ “2”,@ “3”,@ “4”,@ “5”,零]。 在第二组分中,如果用户在第一组分选择US然后我想EUsize2=[NSArray arrayWithObjects:@"11",@"22",@"33",@"44",@"55", nil]; 负载在第二组分中。 同样像EU值然后我想加载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;
    }
}

Answer 1:

只是检查这个方法。它会奏效。

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


Answer 2:

好像你不重装选择器....每次你需要改变在选择器中的数据时,你需要重新加载选择器一样的UITableView。

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

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


文章来源: how to set the data in second component based on first component selection in UIPickerview