2010-07-21 111 views
1

我有点难过,我希望有人能帮助我。我有一个plist,是一组字典。我试图把它读入表格。 plist中看起来是这样的:从包含数组的字典中访问数据

<array> 
    <dict> 
     <key>sectionTitle</key> 
     <string>A</string> 
     <key>rowData</key> 
     <array> 
      <dict> 
       <key>Title</key> 
       <string>Albert Author</string> 
       <key>dText</key> 
       <string>text for detail view</string> 
      </dict> 
      <dict> 
       <key>Title</key> 
       <string>Antony Author</string> 
       <key>dText</key> 
       <string>Descriptive text for detail view</string> 
      </dict> 
      <dict> 
       <key>Title</key> 
       <string>Azerifiel Author</string> 
       <key>dText</key> 
       <string>Text for detail view</string> 
      </dict> 
     </array> 
    </dict> 
    <dict> 
     <key>sectionTitle</key> 
     <string>B</string> 
     <key>rowData</key> 
     <array> 
      <dict> 
       <key>Title</key> 
       <string>Barny Author</string> 
       <key>dText</key> 
       <string>text for detail view</string> 
      </dict> 
      <dict> 
       <key>Title</key> 
       <string>Bazle Author</string> 
       <key>dText</key> 
       <string>text for detail view</string> 
      </dict> 
      <dict> 
       <key>Title</key> 
       <string>Bruno Author</string> 
       <key>dText</key> 
       <string>text for detail view</string> 
      </dict> 
     </array> 
    </dict> 
</array> 

我的问题是这样的:我可以做一个类似的结构减去阵列填充表视图,但我不能让与阵列工作上面的代码。我不知道如何处理代码中的数组,但我无法摆脱它们,因为我希望对最终表进行索引和分区。以下是我的viewDidLoad方法中的代码。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Path to the plist (in the application bundle) 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Tester" ofType:@"plist"]; 

    // Build the array from the plist 
    NSArray *dictArray = [[NSArray alloc] initWithContentsOfFile:path]; 
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init]; 

    for(NSDictionary *dict in dictArray) 
    { 
     DictModel *term = [[DictModel alloc] init]; 
     term.sectionTitle = [dict objectForKey:@"sectionTitle"]; 
     term.Title = [dict objectForKey:@"Title"]; 
     term.dText = [dict objectForKey:@"dText"]; 
     [mutableArray addObject:term]; 
     [term release]; 
    } 

    tableDataSource = [[NSArray alloc] initWithArray:mutableArray]; 
    [mutableArray release]; 
    [dictArray release]; 
    [super viewDidLoad]; 
} 

我有叫DictModel一个单独的类,它看起来是这样的:

@interface DictModel : NSObject { 
    NSString *sectionTitle; 
    NSString *Title; 
    NSString *dText; 
} 

@property(nonatomic, retain) NSString *sectionTitle; 
@property(nonatomic,retain) NSString *Title; 
@property(nonatomic, retain) NSString *dText; 

@end 

我要去哪里错了?我尝试导入数组,但无法使其工作。我认为我在某个地方误解了某些东西。建议非常感谢。

这是我的行方法的单元格。这可能是显示问题的地方。我在一个comentator的要求添加了这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell. 
DictModel *term = [tableDataSource objectAtIndex:indexPath.row]; 
cell.textLabel.text = term.Title; 
cell.detailTextLabel.text = term.dText; 

return cell; 

}

+0

那么究竟是什么问题呢?您目前是否遇到错误或只需要指示如何在编制索引的分组表格中输出plist? – 2010-07-21 01:37:42

+0

我的表格显示为空白。但我知道它正在访问plist,因为如果我将rowAtIndex方法指向它,它将显示sectionTitle字符串。总的来说,我试图让我的头部呈圆形,并且此刻如何订购/加载它们。 – 2010-07-21 02:35:51

+0

您的表视图数据源方法被调用。 – Ideveloper 2010-07-21 06:57:01

回答

1

我前阵子解决了这个问题一点从好心人的帮助就在这里和Mac开发论坛。我以为我会回来分享我为iPhone Dev的新手(像我一样)学习并为这个问题而努力的经验。

请注意:写入最好的我的我所学到试图解决这个问题的认识下,所以有可能是我的描述了一些技术错误。如果你发现任何问题,我会很高兴,如果你指出他们。我参与iphone开发,因为我想学习编程,所以更正欢迎!

首先我的Plist看起来还是一样的。这是一个字典数组。这实际上是一种更简单的做我想做的事的方法,而不是使用我刚开始使用的Dictionary of Arrays。之所以我原来的帖子中的代码看起来很糟糕,是因为它是基于我理解如何使用一个庞大的数组字典来填充表。

数据源已处理,让我们进入我的.h文件。它很简单,看起来像这样:

@interface MyTableViewController : UITableViewController 
{ 
IBOutlet UITableView *myTableView; 
MyDetailViewController *myDetailViewController; 
NSArray *tableDataSource; 

} 

@property (nonatomic, retain) myDetailViewController *myDetailViewController; 
@property (nonatomic, retain) NSArray *tableDataSource; 

@end 

所有漂亮的自我明显。我有我的表格视图和细节表视图的插座。我还有一个数组,我将把.plist读入表格中。

移动到我的.m文件看起来像这样:

@implementation TableViewController 
@synthesize myDetailViewController; 
@synthesize tableDataSource; 

- (void)viewDidLoad { 
[super viewDidLoad]; 

self.title = NSLocalizedString(@"Tester", @"Browse by name"); 

// Path to the plist (in the application bundle) 
NSString *path = [[NSBundle mainBundle] pathForResource:@"Testers" ofType:@"plist"]; 

// Retrieve the plist's root element array 
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path]; 

// Assign the plist array to my instance variable 
self.tableDataSource = array; 
[array release]; 
} 

轴承我原来的问题这一点,我猜重要组成部分,专注于为viewDidLoad方法。基本上,超级后,我将名称分配到表视图的顶部。然后我创建一个名为“路径”的字符串,这个字符串是由我的名为“测试者”的.plist组成的。我创建一个名为'数组'的数组,并使用之前刚刚创建的名为“路径”的字符串的内容对其进行初始化。最后,我将我的.plist数组分配给我的NSArray实例变量'tableDataSource'。这比我最初的尝试要简单得多。有一件事我不再需要单独的类模型。

下一个关键的事情就是我的cellForRowAtIndexPathMethod现在看起来是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell. 
NSDictionary *sectionDictionary = [tableDataSource objectAtIndex:indexPath.section]; 
NSArray *sectionRowArray = [sectionDictionary objectForKey:@"rowData"]; 
NSDictionary *rowDictionary = [sectionRowArray objectAtIndex:indexPath.row]; 
cell.textLabel.text = [rowDictionary objectForKey:@"Title"]; 
return cell; 
} 

这是几乎相同,除了由被配置的小区已经改变,以适应我的阅读新方式的手段之前来自.plist的数据。所以我不再提到Dict模型。 “rowData”和“Title”是我的plist中的键(参见页首)。 “rowData”是每个部分的关键字,“Title”是脚本运行时在单元格中可见的文本。

info的另一个关键位是didSelectRowAtIndexPath方法,该方法确定在选择单元格时将什么推送到详细视图中。它看起来像这样:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
//Get the dictionary of the selected data source. 
NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex:indexPath.section]objectForKey:@"rowData"] objectAtIndex:indexPath.row]; 

MyDetailViewController *controller = [[MYDetailViewController alloc] initWithNibName:@"MyDetailView" bundle:[NSBundle mainBundle]]; 

controller.title = [dictionary objectForKey:@"Title"]; 
controller.dText = [dictionary objectForKey:@"dText"]; 

[self.navigationController pushViewController:controller animated:YES]; 

[controller release]; 

} 

我开始通过获取字典这是在我viewDidLoad方法分配给我的实例变量“tableDataSource”表中的数据源。然后我告诉我的视图细节控制器它在主包中是笔尖,并告诉它,当细节视图被推入时,它应该把关键字“Title”的位置作为该细节视图的特定实例的标题,并且它应该使用.plist中的文本在“dText”键处填充该nib中的TextView。

好吧,就是这样。它对我来说工作得很好,尽管我不能发誓这是做这件事的正确方法。感谢那些帮助我最终获得它的人,我爬上了另一个鼹鼠山!

希望这对其他人有用。

+0

感谢这个很好的解释。我被困在阅读另一个字典内的字典数组。干杯! – nasaa 2012-09-29 15:26:51