【问题标题】:load data in detailviewcontroller from plist?从 plist 在 detailviewcontroller 中加载数据?
【发布时间】:2011-11-10 10:29:30
【问题描述】:

我正在尝试显示来自字典数组的 plist 中的数据:

<array>
    <dict>
        <key>title</key>
        <string>RATATOUILLE</string>
        <key>coverImage</key>
        <string>rat.png</string>
        <key>smallImage</key>
        <string>rat-small.png</string>
        <key>description</key>
        <string>Spray a pan with cooking spray. Brown the onion and the garlic (do not let garlic burn!). Add all the veggies and the bay leaf and simmer covered for 2 hours. Sprinkle on the breadcrumbs and cook another 5 minutes. Remove the bay leaf and serve.
5 servings, 0 POINTS each (if you eat the whole thing, count points for the bread crumbs)</string>
    </dict>

在 rootviewcontroller 中,数据在单元格中显示得很好。在 detailviewcontroller 中,我尝试了相同的方法,但它没有显示任何内容。我究竟做错了什么? 代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *details = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Testdata" ofType:@"plist"]];

    coverImageView.image = [UIImage imageNamed:[details objectForKey:@"coverImage"]];
    titleLabel.text = [details objectForKey:@"title"];
}

我不明白它实际上是如何工作的。在 rootviewcontroller 我有这个方法:

-(id)initWithCoder:(NSCoder *)aDecoder {

    if (self = [super initWithCoder:aDecoder]) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"TestData" ofType:@"plist"];
        arrayIndex = [[NSArray alloc] initWithContentsOfFile:path];

    }
    return self;
}

并且数据是从“arrayIndex”中提取的。细节视图控制器不一样吗? ..................... 编辑: 我想通了,在tableviewcontroller中做了一个字符串:

NSString *selectedItem = [arrayIndex objectAtIndex:indexPath.row];
detailViewController.selectedItem = selectedItem;

并像这样在detailviewcontroller中加载数据:

coverImageView.image = [UIImage imageNamed:[selectedItem valueForKey:@"coverImage"]];
    titleLabel.text = [selectedItem valueForKey:@"title"];

【问题讨论】:

  • 请说出您在“详细信息”字典中得到了什么......

标签: iphone uitableview plist detailsview


【解决方案1】:

我认为你错过了数组部分,你应该从 plist 中加载一个数组,然后在正确的索引处获取字典。顺便说一句,您显然已经在根视图控制器的内存中加载了数据,因此您可以将其作为属性与要读取的索引一起传递给详细视图。

【讨论】:

    猜你喜欢
    • 2011-12-22
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 2013-03-17
    • 2011-12-31
    • 1970-01-01
    相关资源
    最近更新 更多