I’m trying to display data from a plist that is an array of dictionaries:
<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. In the detailviewcontroller I’ve tried the same method and it doesn’t display anything. 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 :
-(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”. Isn’t the same for the detailviewcontroller?
……………….
Edit:
I figured it out, made a string in the tableviewcontroller:
NSString *selectedItem = [arrayIndex objectAtIndex:indexPath.row];
detailViewController.selectedItem = selectedItem;
and loaded data in the detailviewcontroller like this:
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. 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.