我创建了一个data.plist,并希望将根数组(名称)加载到表视图中,然后当您单击每个单元格时,它会将相应的运动数组加载到新的表视图中。我如何将这个方法实现到我的TableViewController中?
我当前的plist有一个根数组,每个肌群有一个字典项(第0项,然后是子名),然后包含肌肉练习的数组。
我的didSelectRowAtIndexPath方法用于推送第二个视图子表的第一个表是:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
ExerciseTableViewController *detailViewController = [[ExerciseTableViewController alloc] initWithNibName:@"AbdominalTableViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
我的第二个表视图控制器的viewDidLoad方法(需要修复)是:
- (void)viewDidLoad
if (exerciseArray == nil)
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"biceps" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.exerciseArray = array;
[array release];
答案 0 :(得分:0)
假设我理解你的问题。
您可以执行以下操作:
- (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];
...
}
在第二个控制器中:
...
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];
...
希望它有所帮助。
在费萨尔评论后编辑:
使用以下plist文件: 要阅读此plist,您可以这样做:
//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];
然后读取第二级,'rootLevel'内容:
NSDictionary *secondLevel = [rootLevel objectAtIndex:0];
NSString *muscle = [secondLevel valueForKey:@"name"]; //"Biceps" string
NSArray *exercises = [secondLevel valueForKey:@"exercises"]; //[exercises objectAtIndex:0] is "Biceps Curl" string