Show data in UIPickerView second component based on first component selection

前端 未结 4 510
悲哀的现实
悲哀的现实 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 08:57

    Usually you do it like that. You create a plist file containing the data of all your countries the result is something like

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

    assuming the following are defined in the properties or somewhere

    NSDictionary *countryClubs;
    NSArray *countries;
    NSArray *clubs;
    

    you then do things like that

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

    This should help you. I hope there is not to much typo mistakes but you should at least get the general idea.

    0 讨论(0)
  • 2020-12-06 09:01

    Look at this, i think it can help you

    1. First of all make sure that you have connected your picker with IBOutlet .

    2. Still if you get the issue you can use

    [thePickerView reloadComponent:(NSInteger)component];

    instead of

    [picker reloadComponent:(NSInteger)component]; 
    
    0 讨论(0)
  • 2020-12-06 09:03

    I think the problem is in your didSelectRow method. You check which country is already selected (in country_flag) before allowing the selection of a new country. I don't think there's any reason to do this.

    Allow any country to be selected. Then set country_flag based on the row that was selected.

    Just do country_flag = row+1;

    You can then have your if statements to set up your label if that's important.

    0 讨论(0)
  • 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 <UIPickerViewDataSource, UIPickerViewDelegate> {
    
        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

    0 讨论(0)
提交回复
热议问题