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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…