Question

I'm having trouble holding on to the contents of an array as I go through a Table View. What I mean is that I have a PList that I'm trying to draw data from for a Detail View once I click on a row in a Table View. Here's what I have in viewDidLoad:

NSString *myfile = [[NSBundle mainBundle] pathForResource:@"MillersDeals" ofType:@"plist"];
NSError *error;
NSData *data = [NSData dataWithContentsOfFile:myfile options:0 error:&error];
self.dealsArray = [NSPropertyListSerialization propertyListWithData:data                                                            options:NSPropertyListMutableContainers format:NULL error:&error];
NSLog(@"%@", self.dealsArray);

Now I've never used NSPropertyListSerialization until earlier when I was suggested it as an answer to a question. I'm not sure if it creates a whole set of different issues or if it needs to be converted back to a Dictionary or Array before I can utilize the information within. It prints out my Plist ok when I NSLog it both times. on the didSelectRowAtIndexPath method I wrote this (I attempt to pass the 'image' from the PList into an Image String that I've set up in the Detail View):

DealsDetailViewController *dealsDetail = [[DealsDetailViewController alloc]initWithNibName:@"DealsDetailViewController" bundle:nil];
NSLog(@"%@", dealsArray);
dealsDetail.petImageString = [[NSString alloc]initWithString:[[[dealsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"Image"]];

The error comes on the dealsDetail.petImageString declaration. Is it the wrong data type that I've set dealsArray to?


Sample of 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">
<array>
    <dict>
        <key>Description</key>
        <string>Here&apos;s the deal</string>
        <key>Image</key>
        <string>Frito-Lays-Jobs.png</string>
        <key>Name</key>
        <string>Frito-Lays</string>
        <key>Thumbnail</key>
        <string>dollars.png</string>
    </dict>
    <dict>
        <key>Description</key>
        <string>Here&apos;s the deal 2</string>
        <key>Image</key>
        <string>Frito-Lays-Jobs.png</string>
        <key>Name</key>
        <string>Frito-Lays 2</string>
        <key>Thumbnail</key>
        <string>dollars.png</string>
    </dict>
</array>

It's just sample stuff (that's why some of the elements are the same).

Était-ce utile?

La solution

This plist you showed is an array of dictionaries, not an array of an array of dictionaries. So something like this should work in your didSelectRowAtIndexPath:

DealsDetailViewController *dealsDetail = [[DealsDetailViewController alloc]initWithNibName:@"DealsDetailViewController" bundle:nil];

dealsDetail.petImageString = [[self.dealsArray objectAtIndex:indexPath.row] objectForKey:@"Image"];

Autres conseils

Just an observation, but in the first code snippet you set a 'dealsArray' whereas in the second code snippet, you try to access the info in 'dataArray'. At what point does the contents of 'dealsArray' get parsed or passed onto 'dataArray'? Are you setting and/or accessing the wrong data structure(s) perhaps?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top