How To Load Plist Into Multiple Table Views
I made a data.plist and want to load the root开发者_开发知识库 array (names) into a table view, then when you click on each cell, it will load their corresponding exercise array into a new table view. How would I implement this method into my TableViewController?
My current plist has a root array, with a dictionary item for each muscle group (Item 0, then child name) then contains an array for the muscle's exercises.
My didSelectRowAtIndexPath Method for the first table that pushes the 2nd view subtable is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
ExerciseTableViewController *detailViewController = [[ExerciseTableViewController alloc] initWithNibName:@"AbdominalTableViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
My viewDidLoad method for the 2nd table view controller (this needs to be fixed) is:
- (void)viewDidLoad
if (exerciseArray == nil)
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"biceps" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.exerciseArray = array;
[array release];
Assuming i understand you question.
You can do something like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *muscle = [muscles objectAtIndex:indexPath.row]; //'muscles' array is the first level of your plist
...
ExerciseTableViewController *detailViewController = [[ExerciseTableViewController alloc] initWithNibName:muscle bundle:nil];
detailViewController.muscle = muscle;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
...
}
And in the second controller:
...
NSString *path = [[NSBundle mainBundle]pathForResource:@"biceps" ofType:@"plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
self.exerciseArray = [plistDict objectForKey:muscle]; //'muscle' is the string you set in the previous controller
[plistDict release];
...
Hope it's help.
Edit after Faisal comment:
Using the following plist file:
For read this plist you can do://Read the plist file into an array (the root element is an array)
NSString *path = [[NSBundle mainBundle]pathForResource:@"Data" ofType:@"plist"];
NSArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
Then for read the second level, the 'rootLevel' content:
NSDictionary *secondLevel = [rootLevel objectAtIndex:0];
NSString *muscle = [secondLevel valueForKey:@"name"]; //"Biceps" string
NSArray *exercises = [secondLevel valueForKey:@"exercises"]; //[exercises objectAtIndex:0] is "Biceps Curl" string
精彩评论