didSelectRowAtIndexpath

didSelectRowAtIndexpath

本文介绍了从Plist设置UILabel文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地将plist数组和字典加载到了表中.我正在尝试在练习的详细信息视图中将exerciseName值设置为UILabel.

I successfully got my plist arrays and dictionaries loaded into my tables. I am trying to set the exerciseName value to a UILabel in my exercise's detail view.

<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.

The exercise Detail is blank right now but will eventually load a UILabel or UITextView in the same exercises's detail view.

我猜我需要做类似的事情

Im guessing I need to do some thing like

label.text =[[self.exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];

对于我的exerciseViewController didSelectIndexAtRow,我有:

For my exerciseViewController didSelectIndexAtRow I have:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:exerciseArray forKey:@"exercises"];
    [def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"exercises"];

,对于detailDateController的ViewDidLoad,我有: 运动详细视图的viewDidLoad

and for ViewDidLoad for detailViewController i have: viewDidLoad for exerciseDetailView

 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 NS编辑默认代码:

Edit with MaserView NSDefault code:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:exerciseArray forKey:@"InfoArray"];
[def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"indexVal"];

使用detailView编辑NSUserDefaults代码:

Edit with detailView NSUserDefaults code:

-(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"];
}

推荐答案

@Faisal:

查看您的代码,我想您的标签在您的tableView下,因为您正在使用indexPath.row为数组建立索引

Looking at your code I suppose that your labels are under your tableView as you are using indexPath.row for indexing the array

您的想法看起来正确.

STEP-1:

从plist检索数据到您的阵列( Plist->阵列)

Retrieve Data from plist into your Array (Plist -> Array)

让我们说如果您的plist名称是Exercise.plist,那么您可以使用以下代码将数据检索到plist中

Let us say if your plist name is Exercise.plist, then you can use the following code to retrieve your data into plist

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Exercise" ofType:@"plist"];
exerciseArray = [NSArray arrayWithContentsOfFile:plistPath];

STEP-2:

从数组获取数据到标签的文本(数组->标签文本)

Get Data from Array into label's text (Array -> Label Text)

然后使用下面的代码从数组值中设置UILabel的文本

Then set the UILabel's text from the array values using below code

label.text =[[exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];

注意:

要在masterView和detailView之间使用相同的数组,可以在masterView的didSelectRowAtIndexPath的NSUserDefaults中设置数组,并在detailView中检索相同的数组.

For using the same array between you masterView and detailView, you can set your array in NSUserDefaults on didSelectRowAtIndexPath in your masterView and retrieve the same array in detailView.

您可以使用以下代码执行此操作:

You can do this using following code:

didSelectRowAtIndexPath方法的masterView中:

In masterView on didSelectRowAtIndexPath method:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:exerciseArray forKey:@"InfoArray"];
[def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"indexVal"];

您可以使用以下代码在detailView中检索该数组:

You can retrieve that array in detailView using below code:

NSMutableArray *exerciseDetailArray = [[[NSUserDefaults standardUserDefaults] objectForKey:@"InfoArray"] mutableCopy];

int indexValue = [[[NSUserDefaults standardUserDefaults] valueForKey:@"indexVal"] intValue];

label.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exercises"];

OR

NSArray *exerciseDetailArray = [[NSArray alloc] initWithContentsOfArray:[[NSUserDefaults alloc] objectForKey:@"InfoArray"]];

int indexValue = [[[NSUserDefaults standardUserDefaults] valueForKey:@"indexVal"] intValue];

label.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exercises"];

希望这对您有所帮助:)

Hope this helps you :)

最终

didSelectRowAtIndexPath:方法替换为SpecificExerciseTableViewController.m

- (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];

}

这肯定可以工作:)

这篇关于从Plist设置UILabel文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:30