我成功地将plist数组和字典加载到我的表中。我正尝试在练习的细节视图中将exerciseName值设置为UILabel。
<array>
<dict>
<key>exercises</key>
<array>
<dict>
<key>exerciseDetail</key>
<string></string>
<key>exerciseName</key>
<string>Ab Crunch Machine</string>
</dict>
<dict>
<key>exerciseDetail</key>
<string></string>
<key>exerciseName</key>
<string>Ab Roller</string>
</dict>
</array>
<key>muscleName</key>
<string>Abdominals</string>
</dict>
练习详细信息现在是空白的,但最终会在同一练习的详细信息视图中加载一个UILabel或UITextView。
我猜我需要做一些事情,比如
label.text =[[self.exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];
编辑:
对于我的exerciseViewController didSelectIndexAtRow,我有:
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:exerciseArray forKey:@"exercises"];
[def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"exercises"];
对于用于detailViewController的ViewDidLoad,我有:用于exerciseDetailView的viewDidLoad
self.navigationItem.title = @"Exercise Detail";
NSMutableArray *exerciseDetailArray = [[[NSUserDefaults alloc] objectForKey:@"exercises"] mutableCopy];
int indexValue = [[[NSUserDefaults alloc] valueForKey:@"exercises"] intValue];
name.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exerciseName"];
控制台输出:
2011-03-19 22:52:08.279 Curl[32300:207] exercisedetalarray: (
{
exerciseDetail = "";
exerciseName = "Ab Crunch Machine";
},
{
exerciseDetail = "";
exerciseName = "Ab Roller";
},
{
exerciseDetail = "";
exerciseName = "Advanced Kettlebell Windmill";
},
{
exerciseName = "Air Bike";
},
{
exerciseName = "Alternate Heel Touchers";
},
{
exerciseName = "Barbell Ab Rollout";
},
{
exerciseName = "Barbell Side Bend";
},
{
exerciseName = "Bent Press";
},
{
exerciseName = "Bent-Knee Hip Raise";
},
{
exerciseName = "Butt-Ups";
},
{
exerciseName = "Cable Crunch";
}
)
2011-03-19 22:52:08.280 Curl[32300:207] index value: 0
使用MaserView NSDefault代码编辑:
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:exerciseArray forKey:@"InfoArray"];
[def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"indexVal"];
使用detailView NSUserDefaults代码编辑:
-(void)viewWillAppear:(BOOL)animated
{
NSMutableArray *exerciseDetailArray = [[[NSUserDefaults alloc] objectForKey:@"InfoArray"] mutableCopy];
int indexValue = [[[NSUserDefaults alloc] valueForKey:@"indexVal"] intValue];
name.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exerciseName"];
}
发布于 2011-03-20 13:15:45
@Faisal:
查看您的代码,我假设您的标签在您的tableView下,因为您正在使用indexPath.row
对数组进行索引
你的想法看起来是对的。
第1步:
将数据从plist检索到数组中(Plist ->数组)
假设您的plist名称为Exercise.plist
,则可以使用以下代码将数据检索到plist中
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Exercise" ofType:@"plist"];
exerciseArray = [NSArray arrayWithContentsOfFile:plistPath];
第2步:
将数组中的数据转换为标签的文本(数组->标签文本)
然后使用以下代码从数组值设置UILabel的文本
label.text =[[exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];
备注:
为了在masterView和detailView之间使用相同的数组,您可以在masterView中的didSelectRowAtIndexPath
上的NSUserDefaults中设置数组,并在detailView中检索相同的数组。
您可以使用以下代码来完成此操作:
在didSelectRowAtIndexPath
方法上的masterView中:
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:exerciseArray forKey:@"InfoArray"];
[def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"indexVal"];
您可以使用以下代码在detailView中检索该数组:
NSMutableArray *exerciseDetailArray = [[[NSUserDefaults standardUserDefaults] objectForKey:@"InfoArray"] mutableCopy];
int indexValue = [[[NSUserDefaults standardUserDefaults] valueForKey:@"indexVal"] intValue];
label.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exercises"];
或
NSArray *exerciseDetailArray = [[NSArray alloc] initWithContentsOfArray:[[NSUserDefaults alloc] objectForKey:@"InfoArray"]];
int indexValue = [[[NSUserDefaults standardUserDefaults] valueForKey:@"indexVal"] intValue];
label.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exercises"];
希望本文能对您有所帮助:)
最终编辑:
在SpecificExerciseTableViewController.m
中将您的didSelectRowAtIndexPath:
方法替换为以下方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.exerciseArray = [[self.exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];
// Pass the selected object to the new view controller.
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:exerciseArray forKey:@"InfoArray"];
[def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"indexVal"];
NSLog(@"Index inexpath:%d",indexPath.row);
int indexValue = [[[NSUserDefaults standardUserDefaults] valueForKey:@"indexVal"] intValue];
NSLog(@"index value master view: %d",indexValue);
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
这肯定是可行的:)
https://stackoverflow.com/questions/5366842
复制相似问题