Showing data in picker

泪湿孤枕 提交于 2019-12-11 15:07:51

问题


Can someone please explain to me why my picker is not showing any data when i load from plist

This is plist:

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

This is part of viewController.m

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

in viewController.h i got

 @property (nonatomic, strong)NSDictionary *countryClubs;
@property (nonatomic, strong)NSArray *countries;
@property (nonatomic, strong)NSArray *clubs;

and in viewController.m i got:

@synthesize countryClubs=_countryClubs;
@synthesize countries=_countries;
@synthesize clubs=_clubs;

If anyone could explain why is picker blank i would appreciate it really much, it seems to that plist is not loaded because if i write :

self.clubs  = [[NSArray alloc]      initWithObjects:@"milimetar",@"centimetar",@"metar",@"kilometar",@"inc",@"stopa",@"jard",@"milja", nil];

picker is showing data.


回答1:


Your viewDidLoad should be:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *plistFile = [bundle URLForResource:@"myPListFile" withExtension:@"plist"];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistFile];

    self.countryClubs = dictionary;
    self.countries = [dictionary allKeys];

    NSString *selectedCountry = self.countries[0];
    NSArray *array = self.countryClubs[selectedCountry];
    self.clubs = array;
}

In your didSelectRow: method, change:

NSArray *array = [countryClubs objectForKey:selectedCountry];

to:

NSArray *array = self.countryClubs[selectedCountry];

Also get rid of all of your @synthesize lines. If you are using any tutorial that talks about using @synthesize then it is out of date. Also get rid of any ivars you may have explicitly declared for your properties.



来源:https://stackoverflow.com/questions/21173193/showing-data-in-picker

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