Search code examples
xcodeplistnsdictionarytableviewcell

How to use a plist to fill a iOS TableView in XCode 4


Possible Duplicate:
How to populate a TableView with plist content using XCode and 4 storyboard

I've done these steps:

1: Made a tableviewcontroller (WinesViewController)

2: Added a prototype cell with title & subtitle labels: namelabel & districtlabel. Cell identifier: wineCell

3: Made the Wine.plist with 10 wines (dictionaries) (Dictionary key = name of the wine)

The dictionaries are containing 18 strings with string keys: Name, District, Image, Alcohol etc, and 1 array called Price with Price strings.

4: Made a Wine.h/.m subclass of NSObject. The .h:

#import <Foundation/Foundation.h>

@interface Wine : NSObject {

    NSString *wineImage;
    NSString *wineName;
    NSString *wineDistrict;
    NSString *wineCountry;
    NSString *wineFlag;
    NSString *wineFylde;
    NSString *wineFriskhet;
    NSString *wineGarvesyre;
    NSString *wineColor;
    NSString *wineSmell;
    NSString *wineTaste;
    NSString *wineSuits;
    NSString *wineGrapes;
    NSString *wineAlcohol;
    NSString *wineSugar;
    NSString *wineAcid;
    NSString *wineProducer;
    NSMutableArray *winePrice;
    NSString *wineFact;
}

@property (copy, nonatomic) NSString *wineImage;
@property (copy, nonatomic) NSString *wineName;
@property (copy, nonatomic) NSString *wineDistrict;
@property (copy, nonatomic) NSString *wineCountry;
@property (copy, nonatomic) NSString *wineFlag;
@property (copy, nonatomic) NSString *wineFylde;
@property (copy, nonatomic) NSString *wineFriskhet;
@property (copy, nonatomic) NSString *wineGarvesyre;
@property (copy, nonatomic) NSString *wineColor;
@property (copy, nonatomic) NSString *wineSmell;
@property (copy, nonatomic) NSString *wineTaste;
@property (copy, nonatomic) NSString *wineSuits;
@property (copy, nonatomic) NSString *wineGrapes;
@property (copy, nonatomic) NSString *wineAlcohol;
@property (copy, nonatomic) NSString *wineSugar;
@property (copy, nonatomic) NSString *wineAcid;
@property (copy, nonatomic) NSString *wineProducer;
@property (retain, nonatomic) NSMutableArray *winePrice;
@property (copy, nonatomic) NSString *wineFact;

@end

And synthesized them all inn wine.m

And then I've tried different combinations of codes from Apples Propery List Programming Guide, http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1 and answers from here, to fill the tableview cells with the Name and District strings from the plist as title/subtile. Now I've created a mess and confused myself, I dont even want to post the code from WinesViewController.h/.m. It's better to just start over.

Later I will add the images too, and use the rest of the info from the plist to create the WinesDetailViewController, but making the tableView seems to be enough to begin with.. Can someone help me please?

I want the wines to show a-z in the tableview, just 1 section to begin with and i will add sections later.. First, I need to make this ground structure finished!


Solution

  • The simplest way is to use:

    NSDictionary* wines = [NSDictionary dictionaryWithContentsOfURL:<URL to plist file>];
    

    That will create a dictionary containing everything that was in the plist. You can then step through the keys of the dictionary (which will be the names of the wines), like this:

    NSEnumerator* keyEnum = [wines keyEnumerator];
    NSDictionary* nextWine = nil;
    while ((nextWine = [keyEnum nextObject]) != nil)
    {
        // pull out the data for each wine, such as its image, district, country, etc. and
        // put them into your model object.
    }