从包含数组的字典数组中访问数据

时间:2010-07-21 01:23:12

标签: iphone arrays dictionary plist

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

我的问题是:我可以制作一个类似的结构,减去Arrays填充表视图,但我不能使用数组的上述代码工作。我不知道如何处理代码中的数组,但我无法摆脱它们,因为我希望对最终表进行索引和分段。这是我的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;

}

1 个答案:

答案 0 :(得分:1)

我刚才在这里和mac开发论坛的好人们的帮助下解决了这个问题。我以为我会回来分享我为其他任何对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方法。基本上,在超级之后我将名称分配给表视图的顶部。然后我创建了一个名为'path'的字符串,它由我的.plist组成,名为“Testers”。我创建了一个名为'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”是脚本运行时在单元格中可见的文本。

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'的表数据源的字典。然后我告诉我的视图细节控制器它是mainbundle中的nib并且告诉它当推送详细视图时它应该把关键字“Title”的位置中的任何内容作为详细视图的特定实例的标题并且它应使用键“dText”中的.plist中的文本填充该nib中的TextView。

嗯,就是这样。它对我来说很好,但我不能发誓这是正确的做法。感谢帮助我最终得到它的人们,我爬上了另一个鼹鼠山!

希望这对其他人有用。