我有点难过,我希望有人能帮助我。我有一个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;
}
那么究竟是什么问题呢?您目前是否遇到错误或只需要指示如何在编制索引的分组表格中输出plist? – 2010-07-21 01:37:42
我的表格显示为空白。但我知道它正在访问plist,因为如果我将rowAtIndex方法指向它,它将显示sectionTitle字符串。总的来说,我试图让我的头部呈圆形,并且此刻如何订购/加载它们。 – 2010-07-21 02:35:51
您的表视图数据源方法被调用。 – Ideveloper 2010-07-21 06:57:01