0

可能重复:
如何使用 XCode 和 4 故事板使用 plist 内容填充 TableView

我已经完成了这些步骤:

1:做了一个tableviewcontroller(WinesViewController)

2:添加了一个带有标题和副标题标签的原型单元格:名称标签和地区标签。单元格标识符:wineCell

3:用 10 种酒(字典)制作 Wine.plist(字典键 = 酒名)

字典包含 18 个带有字符串键的字符串:Name、District、Image、Alcohol 等,以及 1 个名为 Price 和 Price 字符串的数组。

4:制作了 NSObject 的 Wine.h/.m 子类。.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

并将它们都合成了inn wine.m

然后我尝试了 Apples Propery List Programming Guide 中的不同代码组合,http: //www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1和此处的答案,以填写表格视图单元格,其中 plist 中的名称和区域字符串作为标题/子图块。现在我把自己弄得一团糟,我什至不想发布来自 WinesViewController.h/.m 的代码。最好重新开始。

稍后我也会添加图像,并使用 plist 中的其余信息来创建 WinesDetailViewController,但制作 tableView 似乎就足够了。有人可以帮我吗?

我希望葡萄酒在 tableview 中显示 az,只有 1 个部分开始,稍后我将添加部分。首先,我需要完成这个地面结构!

4

1 回答 1

1

最简单的方法是使用:

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

这将创建一个包含 plist 中所有内容的字典。然后,您可以逐步浏览字典的键(将是葡萄酒的名称),如下所示:

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.
}
于 2012-06-10T14:33:26.843 回答