Show data in UIPickerView second component based on first component selection

前端 未结 4 515
悲哀的现实
悲哀的现实 2020-12-06 08:42

I am using a picker with two 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.

4条回答
  •  渐次进展
    2020-12-06 09:08

    your code.. with minor modifications..

    didSelectRow

    - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
        if (component == 0) {
            club=[[NSString alloc] initWithFormat:@"%@" , [Nations objectAtIndex:row]];
            [pickerView reloadComponent:1];
              }
    

    }

    after that...

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

    and

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

    in h file I have

    @interface ViewController : UIViewController  {
    
        IBOutlet UIPickerView *pickerView;
        NSMutableArray *arrayColors;
        NSMutableArray *Nations;
        NSMutableArray *England;
        NSMutableArray *Espana;
        NSMutableArray *Germany;
        NSString *club;
    }
    

    after that.. you must connect de pickerView to yours ViewController (dataSource and delegate) enter image description here

提交回复
热议问题