Connect a prototype cell with a plist in xcode 4

2019-07-29 09:41发布

问题:

I'm making my first iphone application and it's a wine chart application. It's with storyboard in xcode 4.2. It's based on a tab bar with 4 tabs: info, wine chart, search, favourites. I'm making the wine chart at the moment, In this tab I've put a TableView with a prototype cell and I want to fill this cell with name, district and image of the wines. So far I've done this:

1: Linked the second tab to a navigation controller

2: Linked the navigation controller to the TableView (WinesViewController)

3: Created the WinesViewController.h/.m class

4: Added a prototype cell with identifier: wineCell and title & subtitle labels with text: name & district.

5: Made a folder with all the images of the bottles WineName.png

6: Created a plist: Wine.plist with 1 dictionary for each wine, dictionary key = wine name. The dictionaries contains 19 strings with keys "Name", "District" "Image", "Acid level" etc.

This is the plist:

http://www.flickr.com/photos/80427098@N07/7372450200/in/photostream

That's all I've done so far, and now I want to fill the cell labels in the tableview with the strings in the plist, sorted alphabetically in sections. I'm new to this so I dont know which codes to use and where to put them. Can someone help me understand more of how to populate a tableview with information from the plist?

I've been told that I shouldn't put the code directly in the WinesViewController.h/.m. So then I've been told that I can create a subclass of NSObject (let's call it WineObject.h/.m), put some NSDictionary / NSEnumerator codes in here, and then somehow link this to the WinesViewController prototype cell. But I really don't know how! I'm not that experienced yet! I really hope someone can give me help with the codes, where to put them and how to link it all up. That it would be fantastic. I'm starting to understand X-Code and Objective C coding a little bit now (finally!) but this is to advanced yet, and some help now that I'm stuck would give me a lot to work with! Directions to helpful tutorials, sample projects etc is also appreciated.

Let me know if there is something more you need to know in order to answer my question! I haven't written a single line of code in the WinesViewController or WineObject so I really need the whole explanation..

Later I'll use the information from the same plist in a DetailViewController (a fullscreen view with info on the wines) thats why there's so many strings in the plist when I'm only using 3 values in the tableview cells. The search function will also be made on the data from this plist. Thank you for all useful help!

回答1:

You should start by reading about how to use the model-view-controller paradigm in Objective-C. This will explain how to separate your data from views of that data. Once you understand that, you'll be in a better position to understand the rest of this.

I would suggest making one change to your plist file. Rather than having the top level be a dictionary where each wine name is a key, and the value for each key is a dictionary containing information about the wine, I would make the top level be an array of dictionaries. Each wine would be one element in the array, and would be a dictionary containing all the information you currently have plus the wine's name. This will more closely correspond to what your data model will look like. Something like this:

<array>
    <dict>
        <key>Name</key>
        <string>J. P. Chenet Merlot</string>
        <key>Image</key>
        <string>JPChenet.png</string>
        ... the rest of the keys and values for this wine ...
    </dict>

    <dict>
        <key>Name</key>
        <string>Campo Viejo Crianza</string>
        ... the rest of the keys ...
    </dict>
</array>

Each wine object would contain an instance variable for each element in the dictionary. (So one for image, one for name, one for district, etc.) Something like this:

@class WineObject : NSObject { 
    NSString* name;
    NSString* imagePath;
    NSString* district;
    // ... etc.
};

Your model could just be an NSArray of WineObjects. You could read the objects out of the plist using the method I mentioned in your other question. It would look something like this:

NSMutableArray* wineModel = [[NSMutableArray alloc] init];
NSArray* wines = [NSArray arrayWithContentsOfURL:<url of the plist file>];
NSEnumerator* wineEnum = [wine objectEnumerator];
NSDictionary* nextWine = nil;
while ((nextWine = [wineEnum nextObject]) != nil)
{
    // Get the wine data from the dictionary
    WineObject* newWine = [WineObject wineWithDictionary:nextWine];

    // Add the next wine to our model
    [wineModel addObject:newWine];
}

Your WineObject class would then have a class initializer something like this:

+ (id)wineWithDictionary:(NSDictionary*)wineDict
{
    name = [wineDict objectForKey:@"Name"];
    district = [wineDict objectForKey:@"District"];
    imagePath = [wineDict objectForKey:@"Image"];
    // ... extract the rest of the values from the dictionary ...
}