how to set the data in second component based on first component selection in UIPickerview

一曲冷凌霜 提交于 2019-12-13 18:53:07

问题


I am using a UIpicker with three 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 in second component. i used array for first component value like @"UK",@"US",@"EU".now i want if user selected UK in first component then i want to load UKsize1=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil]; in second component , if user selected US in first component then i want EUsize2=[NSArray arrayWithObjects:@"11",@"22",@"33",@"44",@"55", nil]; load in second component. same like for EU value then i want to load USsize3=[NSArray arrayWithObjects:@"111",@"222",@"333",@"444",@"555", nil];. i want second component value changed based on the first component , third component value is fixed.

this code i tried but i am not getting exact value.

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


回答1:


just check this method .it will work .

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



回答2:


It seems like you are not reloading the picker....Every time you need to change the data in the picker you need to reload the picker just like UITableView.

// Reloading whole view or single component

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


来源:https://stackoverflow.com/questions/24453710/how-to-set-the-data-in-second-component-based-on-first-component-selection-in-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!