0

現在、次の plist を使用してドリルダウン テーブル ビューを作成しています。ただし、子の配列を DetailViewController に渡す際に問題が発生しています。

<plist version="1.0">
<dict>
    <key>Food</key>
    <array>
        <dict>
            <key>Name</key>
            <string>Food</string>
            <key>Description</key>
            <string>Description about Food.</string>
            <key>IconLink</key>
            <string>WFImage.png</string>
            <key>Children</key>
            <array>
                <dict>
                    <key>ChildName</key>
                    <string>Burgers</string>
                    <key>ChildDescription</key>
                    <string>Description of Burgers</string>
                    <key>ChildIconLink</key>
                    <string>WFImage.png</string>
                    <key>ChildImageLink</key>
                    <string>WFBanner.png</string>
                </dict>
                <dict>
                    <key>ChildName</key>
                    <string>Hot Dogs</string>
                    <key>ChildDescription</key>
                    <string>Description of Hot Dogs</string>
                    <key>ChildIconLink</key>
                    <string>WFImage.png</string>
                    <key>ChildImageLink</key>
                    <string>WFBanner.png</string>
                </dict>
            </array>
        </dict>

以下を使用して、子の配列を詳細ビューに渡すことができました (正しい量の行が表示されます) が、子要素を抽出することはできません。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        if ([segue.identifier isEqualToString:@"showMenuDetail"]) {

            NSIndexPath *indexPath = [self.menuTableView indexPathForSelectedRow];
            HomeDetailViewController *HDViewController = segue.destinationViewController;
            HDViewController.foodChildren = [[menu objectAtIndex:indexPath.row] objectForKey:@"Children"];
            HDViewController.foodName = [[menu objectAtIndex:indexPath.row] objectForKey:@"Name"];

            NSLog(@"Food Children: %@", HDViewController.foodChildren);
    }

}

詳細ビューの cellForRowAtIndexPath で次のことを試してみると:

children = [foodChildren objectForKey:@"ChildName"];
cell.textLabel.text = [children objectAtIndex:indexPath.row];

次のエラーが表示されます。

理由: '-[__NSCFArray objectForKey:]: 認識されないセレクターがインスタンス 0x200a7870 に送信されました'

どんな助けでも大歓迎です!!!

4

3 に答える 3

0

infectionChildrenのように見えますNSArray(おそらく で取得し[yourPlist objectForKey:@"Children"]ましたが、投稿したコードには表示されません)。infectionChildrenが NSDictionaries の配列である場合、objectForKey に応答しない[infectionChildren objectForKey:@"ChildName"]ため、単に使用することはできません。NSArray

次のように、配列を繰り返し処理して子の名前を抽出する必要があります。

NSMutableArray *children = [[NSMutableArray alloc] initWithCapacity:[infectionChildren count]];
for (NSDictionary *childDictionary in infectionChildren) {
    [children addObject:[childDictionary objectForKey:@"ChildName"]];
}
于 2013-05-24T21:36:18.993 に答える
0

「objectForKey」メッセージを NSArray に送信しようとしているように見えるため、このエラーが発生しています。

あなたの plist の設定とコードによると、HDViewController.foodChildrenは配列なので、私の質問は、それをどのように変数「infectionChildren」に渡すのですか?

HDViewController.foodChildren NSArray のオブジェクトをinfectionChildrenに解析する必要があります

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

NSDictionary *infectionChildren = [self.foodChildren objectAtIndex:indexPath.row]; 

}
于 2013-05-24T21:41:12.983 に答える
0

これは、 NSArray に対して NSDictionary メソッドを使用しているためです。objectForKey:は辞書に使用されますが、使用しているinfectionChildrenは配列です。

お役に立てば幸いです。

于 2013-05-24T21:44:56.267 に答える