将对象从plist传递到表格单元格

时间:2011-10-03 14:34:36

标签: iphone objective-c xcode plist

我想就下一步做什么寻求建议,以便将数据列表从Plist传递给数组。

我想将plist数据传递给我的tableview BrowseViewController.m,但我不知道该怎么做。

我试过NSlog,plist正在传递信息,但我不知道wat是我的下一步。

有人能教我吗?非常感谢。

这是我的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>AudioFileDescriptions</key>
    <array>
        <string></string>
        <string></string>
        <string></string>
        <string></string>
    </array>
    <key>CommonName</key>
    <string>Cane Toad </string>     
    <key>ScientificName</key>
    <string>Bufo marinus </string>
    <key>Species</key>
    <string>marinus</string>
</dict>

我的应用代表

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:   (NSDictionary *)launchOptions
  {
   // Override point for customization after application launch.
   // Add the tab bar controller's current view as a subview of the window
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

//load frog information from plist
NSBundle *bundle = [NSBundle mainBundle];
NSString *eventsPath = [bundle pathForResource:@"Frog" ofType:@"plist"];
frogObject = [Frog frogObjectWithContentsOfFile:eventsPath];

return YES;
}

Frog.m

+ (id)frogObjectWithContentsOfFile:(NSString *)aPath
{
Frog *frogObject = [[Frog alloc] init];

if (frogObject) {
    // load all frogs from plist
    NSMutableArray *allFrogsArray = [NSArray arrayWithContentsOfFile:aPath];
    //NSDictionary *allFrogsArray = [NSDictionary dictionaryWithContentsOfFile:aPath];
    if (allFrogsArray == nil) {
        [frogObject release];
    }

    [frogObject.allFrogs addObjectsFromArray:allFrogsArray];

    // iterate through each event and put them into the correct array
    NSEnumerator *frogsEnumerator = [allFrogsArray objectEnumerator];
    NSDictionary *currentFrog;
    while (currentFrog = [frogsEnumerator nextObject]) {
        [frogObject addFrog:currentFrog];
    }
   // NSLog(@"FrogObject %@",allFrogsArray);
}
return frogObject;
}

- (void)addFrog:(NSDictionary *)anFrog;
{

    NSLog(@"anFrog Value %@",anFrog);
}

1 个答案:

答案 0 :(得分:1)

您应该重新编写代码的结构,为什么“FrogObject”会为所有青蛙保存一个数组?它在语义上令人困惑,因为您期望FrogObject保存单个实体的数据,而不是整个集合。一种方法是让FrogObject知道如何加载多个青蛙,但返回数组。

例如,青蛙对象看起来像:

@interface Frog : NSObject

@property (nonatomic,retain) NSString* commonName;
@property (nonatomic,retain) NSString* scientificName
@property (nonatomic,retain) NSString* species;

+ (NSArray*)frogsFromFile:(NSString*)filePath;

- (Frog*)initWithDictionary:(NSDictionary*)frogDictionary;

@end

frogsFromFile:可以创建多个青蛙实例(每个青蛙都有自己的带有initWithDictionary:的子词典),但最重要的是,它返回它,它不会保留它。

接下来,您可以使用它在tableView中显示它,例如(BrowseViewController.h):

@interface BrowseViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *frogs;
}

BrowseViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    frogs = [Frog frogsFromFile:[[NSBundle mainBundle] pathForResource:@"Frog" ofType:@"plist"]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [frogs count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"frogCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
    }

    Frog *frog = [frogs objectAtIndex:indexPath.row];

    cell.textLabel.text = frog.commonName;
}