简体   繁体   English

从plist加载到detailviewcontroller中的数据?

[英]load data in detailviewcontroller from plist?

I'm trying to display data from a plist that is an array of dictionaries: 我正在尝试显示来自字典列表的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>

in the rootviewcontroller the data displays just fine in the cells. 在rootviewcontroller中,数据在单元格中显示得很好。 In the detailviewcontroller I've tried the same method and it doesn't display anything. 在detailviewcontroller中,我尝试了相同的方法,但未显示任何内容。 What am I doing wrong? 我究竟做错了什么? Here's the code: 这是代码:

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

what I don't understand is how it works actually. 我不了解的是它实际上是如何工作的。 In the rootviewcontroller I have this method : 在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;
}

and the data is pulled from the "arrayIndex". 并从“ arrayIndex”中提取数据。 Isn't the same for the detailviewcontroller? 与detailviewcontroller不同吗? ................... Edit: I figured it out, made a string in the tableviewcontroller: ...................编辑:我想通了,在tableviewcontroller中做了一个字符串:

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

and loaded data in the detailviewcontroller like this: 并将数据加载到detailviewcontroller中,如下所示:

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

I think you miss the array part, you should load an array form the plist and then get the dictionary at proper index. 我认为您错过了数组部分,应该从plist加载数组,然后以适当的索引获取字典。 Incidentally you've apparently already loaded the data in memory in the rootviewcontroller, so you could just pass it as a property to the detail view together with the index to be read. 顺便说一句,您显然已经将数据加载到了rootviewcontroller的内存中,因此您可以将其作为属性与要读取的索引一起传递给详细视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM