【问题标题】:Accessing data from an Array of dicionaries containing an array从包含数组的字典数组中访问数据
【发布时间】:2010-07-21 01:23:12
【问题描述】:

我有点难过,我希望有人能帮助我。我有一个 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

我哪里错了?我尝试导入数组但无法正常工作。我想我在某个地方误解了一些东西。非常感谢您的建议。

这是我的行方法单元格。这可能是显示问题所在。我应评论员的要求添加了这个:

- (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;

}

【问题讨论】:

  • 那么到底是什么问题呢?您目前是否遇到错误或只是需要指导如何在索引分组表中输出您的 plist?
  • 我的表格显示为空白。但我知道它可以访问 plist,因为如果我将 rowAtIndex 方法指向它,它将显示 sectionTitle 字符串。总的来说,我目前正在尝试了解 plist 以及如何订购/加载它们。
  • 您的表视图数据源方法是否被调用。
  • 我认为有两个 [super viewdidLoad] 方法被调用。不确定这是否是一个错误。只是想指出
  • 是的。我不是 100% 我做对了。我想我是,但我也知道我想用来填充 tableview 的字典包含在字典内的数组中。我需要以这种方式构造它,以便我可以拥有一个分段索引表,但这让我感到困惑。但没有混淆,没有学习曲线;-) 我已经在上面发布了表格日期源方法。

标签: iphone arrays dictionary plist


【解决方案1】:

不久前,我在这里和 mac Dev 论坛上的好人的帮助下解决了这个问题。我想我会回来分享我所学到的知识,以帮助那些刚接触 iphone Dev(比如我)并在这个问题上苦苦挣扎的人。

请注意: 以下内容是根据我在尝试解决此问题时所学到的知识编写的,因此我的描述中可能存在一些技术错误。如果你发现了,我会很高兴你指出它们。从事iphone开发是因为想学编程,欢迎指正!

首先,我的 Plist 看起来还是一样。这是一个字典数组。这确实是一种比使用我最初开始使用的数组字典更简单的方法来做我想做的事情。我原始帖子中的代码看起来如此糟糕的原因是因为它是基于我对如何使用数组字典填充表的理解,这很笨重。

数据源处理完毕,让我们进入我的 .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 方法。基本上,在超级之后,我将名称分配给表格视图的顶部。然后,我创建了一个名为“路径”的字符串,它由名为“Testers”的.plist 组成。我创建了一个名为“array”的数组,并使用我之前刚刚创建的名为“path”的字符串的内容对其进行初始化。最后,我将我的 .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”是脚本运行时单元格中可见的文本。

信息的另一个关键位是 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”。然后我告诉我的视图详细信息控制器它是主包中的 nib,并告诉它当详细视图被推送时,它应该将键“标题”位置中的任何内容作为详细视图的特定实例的标题,并且它应该使用键“dText”处的 .plist 中的文本填充该笔尖中的 TextView。

嗯,就是这样。它对我来说很好,虽然我不能发誓这是正确的做法。感谢最后帮助我得到它的人,我又爬了一座鼹鼠山!

希望这对其他人有用。

【讨论】:

  • 感谢您的精彩解释。我被困在阅读另一本字典中的字典数组。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 2011-05-04
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多