0

「タイトル」キーでUITableViewをコンパイルするplistがあります。plistは下にあります。テーブルビューは正しく入力されます。これを使用してViewControllerをプッシュします。

- (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です)。「LAPlace1」の行が選択されているときに@「3001Sunset」が表示されないのはなぜですか。

- (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です:行
  • 2番目の項目は、次のキーを持つ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 に答える