从plist加载detailviewcontroller中的数据?

时间:2011-11-10 10:29:30

标签: iphone uitableview plist detailsview

我正在尝试显示来自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”中提取数据。 detailviewcontroller不一样吗? ................... 编辑: 我想通了,在tableviewcontroller中创建了一个字符串:

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

并在detailviewcontroller中加载数据,如下所示:

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

1 个答案:

答案 0 :(得分:0)

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