2011-10-03 99 views
0

我想就下一步做什么以便将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

你或许应该返工structu为什么“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; 
} 
+0

喜可以, 感谢您的答复,你将能够教我如何编写+(NSArray *)函数frogsFromFile:(NSString *)filePath; and - (Frog *)initWithDictionary:(NSDictionary *)frogDictionary; 我该如何使用@property(nonatomic,retain)NSString * species; ???? – Desmond

相关问题