0

我有一个通过键“Title”编译 UITableView 的 plist,plist 在下面。表格视图正确填充,我用这个推送一个视图控制器:

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath;
{
    LocationsReviewViewController *nextViewController = [[LocationsReviewViewController alloc]init];
    NSDictionary *rowData = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
    nextViewController.title = [rowData objectForKey: @"Title"];
    [self.navigationController pushViewController: nextViewController animated: YES];
}

我如何在nextViewController上面保留一些关于选择表中哪个对象的信息?另外,如何在新nextViewController视图上显示每个子树键中包含的字符串?ViewDidLoad 应该是什么样的?

这是 nextViewController 的 ViewDidLoad 当前的样子(描述是 UILabel)。为什么选择“LA Place 1”行时不显示@“3001 Sunset”:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"]];
    NSDictionary *subtree = [dictionary objectForKey: @"Subtree"];
    description.text = [NSString stringWithFormat:@"%@",[subtree objectForKey:@"Item 2"]];
}

<array>
    <dict>
        <key>Rows</key>
        <array>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>Los Angeles, CA</string>
                    <string>Tuesday</string>
                    <string>3001 Sunset</string>
                </array>
                <key>Title</key>
                <string>LA Place 1</string>
            </dict>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>New York, NY</string>
                    <string>Sunday</string>
                    <string>1400 Broadway</string>
                </array>
                <key>Title</key>
                <string>NYC Place 1</string>
            </dict>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>Austin, TX</string>
                    <string>Sunday</string>
                    <string>2400 Lamar Blvd</string>
                </array>
                <key>Title</key>
                <string>Austin Place 1</string>
            </dict>
        </array>
        <key>Title</key>
        <string>Section1</string>
    </dict>
</array>
</plist>
4

1 回答 1

0

查看您的 Data3.plist 我看到它是一个包含 2 个项目的字典。

  • 第一项是带有键的 NSArray:行
  • 第二项是带有键的 NSString:Title

所以在你- (void)viewDidLoad的代码中是不正确的。这应该是正确的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"]];
    // get the Rows object
    NSArray *rows = [dictionary objectForKey:@"Rows"];
    // The array object contains 3 NSDictionary
    NSDictionary *firstItem = [rows objectAtIndex:0]; // I am just getting the first item
    // once I have the dictionary, which have 2 items, Subtree & title 
    NSArray *subtree = [firstItem objectForKey: @"Subtree"];
    // subtree object is NSArray with 3 NSString object, therefore you have to use index
    description.text = [NSString stringWithFormat:@"%@",[subtree objectAtIndex:2]];
}

Data3.plist 截图

现在,如果你想将数据传递给下一个控制器,你应该为你的LocationsReviewViewController对象添加一个属性。

在您的 LocationsReviewViewController.h 中:

// declare an ivar
NSDictionary *data;

// declare property
@property (nonatomic, retain) NSDictionary *data;

在您的 LocationsReviewViewController.m 中:

@synthesize data;

当您分配 LocationsReviewViewController 对象时:

- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
{
    NSDictionary *rowData = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
    LocationsReviewViewController *nextViewController = [[LocationsReviewViewController alloc]init];
    nextViewController.data = rowData;  // pass in your dictionary to your controller
    nextViewController.title = [rowData objectForKey: @"Title"];
    [self.navigationController pushViewController: nextViewController animated: YES];
}
于 2011-05-02T20:49:21.357 回答