在选择器中显示数据 [英] Showing data in picker

查看:83
本文介绍了在选择器中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么我从plist加载时选择器没有显示任何数据

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

这是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>` 

这是viewController.m的一部分

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

在viewController.h中,我

in viewController.h i got

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

在viewController.m中,我得到了:

and in viewController.m i got:

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

如果有人能解释为什么选择器为空白,我将非常感谢,看来plist并未加载,因为如果我写的话:

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

选择器正在显示数据.

推荐答案

您的viewDidLoad应该是:

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

在您的didSelectRow:方法中,更改:

NSArray *array = [countryClubs objectForKey:selectedCountry];

收件人:

NSArray *array = self.countryClubs[selectedCountry];

也要摆脱所有@synthesize行.如果您使用的是有关使用@synthesize的任何教程,那么它已经过时了.此外,请摆脱掉可能已为属性明确声明的所有ivars.

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.

这篇关于在选择器中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆