Hi all,
I am trying to build a two level table view, under which is a detail view. I am using a plist to load the data.
The problem is getting the nested arrays into the second table. At the moment it just crashes. I think it is something to do with using an array within the array but am not sure how to sort this out.
any help would be amazing. I'm kind of noobyish so sorry if this question is obvious.
here is the RootViewController
Code:
#import "RootViewController.h"
#import "DrillDownAppAppDelegate.h"
#import "DetailViewController.h"
@implementation RootViewController
@synthesize tableDataSource, CurrentTitle, CurrentLevel;
- (void)viewDidLoad {
[super viewDidLoad];
if(CurrentLevel == 0) {
NSArray *tempArray = [[NSArray alloc] init];
self.tableDataSource = tempArray;
[tempArray release];
DrillDownAppAppDelegate *AppDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
self.navigationItem.title = @"Categories";
}
else
self.navigationItem.title = CurrentTitle;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tableDataSource count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:@"Title"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the dictionary of the selected data source.
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSArray *Children = [dictionary objectForKey:@"Children"];
if([Children count] == 0) {
NSString *myLabel = [dictionary objectForKey:@"Title"];
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
dvController.myLabel = myLabel;
dvController.title = [dictionary objectForKey:@"Title"];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
else {
//Prepare to tableview.
RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
//Increment the Current View
rvController.CurrentLevel += 1;
//Set the title;
rvController.CurrentTitle = [dictionary objectForKey:@"Title"];
//Push the new table view on the stack
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
[rvController release];
}
}
- (void)dealloc {
[CurrentTitle release];
[tableDataSource release];
[super dealloc];
}
@end
here is the DrillDownAppAppDelegate
Code:
#import "DrillDownAppAppDelegate.h"
#import "RootViewController.h"
//#import "QuotesTableViewController.h"
@implementation DrillDownAppAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize data;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
self.data = tempDict;
[tempDict release];
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}
- (void)dealloc {
[data release];
[navigationController release];
[window release];
[super dealloc];
}
@end
and here is the plist file
<dict>
<key>Rows</key>
<array>
<dict>
<key>Title</key>
<string>ChildTitle1</string>
<key>Children</key>
<array>
<string>no 1</string>
<string>yes 2</string>
<string>yes 3</string>
<string>yes 4</string>
<string>yes 5</string>
<string>yes 6</string>
<string>yes 7</string>
<string>yes 8</string>
<string>yes 9</string>
<string>yes 11</string>
<string>yes 10</string>
<string>yes 12</string>
<string>yes 13</string>
<string>yes 14</string>
<string>yes 15</string>
</array>
</dict>
If anyone has any ideas at all that would be amazing and I would be very grateful indeed. Thanks.