开发者

Passing Objects to table cell from plist

i will like to seek advise on what should i do next in order to pass a list of data from Plist to an array.

i will like to pass the plist data to my tableview BrowseViewController.m, but i do not know how to do it.

i tried NSlog, the plist are passing info over but i don't know wat is my next step.

can anyone teach me ? thanks alot.

this is my 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>

my app delegate

  - (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 plis开发者_StackOverflow社区t
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);
}


You should probably rework the structure of the code, why does the "FrogObject" holds an array to all the frogs? It's semantically confusing, as you expect that a FrogObject holds the data for a single entity, not the whole set. A way would be to make the FrogObject know how to load multiple frogs, but return the array.

For example, the frog object would look like:

@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

The frogsFromFile: can create multiple instances of frogs (each one with its own sub-dictionary with initWithDictionary:), but most importantly, it returns it, it doesn't hold it.

Next, you can use to present it in a tableView, for example (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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜