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

It looks to me like infectionChildren is an NSArray (probably got with [yourPlist objectForKey:@"Children"], though I don't see that in the code you posted). If infectionChildren is an array of NSDictionaries then you can't just use [infectionChildren objectForKey:@"ChildName"] because NSArray doesn't respond to objectForKey.

You should iterate over your array to extract the child names, like:

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 回答