Hi, I'm a bit stumped and I'm hoping someone can help me. I've got a plist which is an array of dictionaries. I'm trying to read it into a table. The plist looks like this:
<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>
My problem is this: I can make a similar structure minus the Arrays populate a table view, but I can't make the above code with the arrays work. I'm not sure how to deal with the arrays in the code, yet I can't get rid of them because I want the final table to be indexed and sectioned. Here's the code from my viewDidLoad method.
- (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];
}
I've got a separate class called DictModel which looks like this:
@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
Where am I going wrong? I tried importing the Arrays but couldn't get it to work. I Figure I'm misunderstanding something somewhere. Advice is much appreciated.
Here's my cell for row method. This might be where the display problem is. I've added this at the request of a 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;
}