【问题标题】:Passing NSArray to Detail View using Plist?使用 Plist 将 NSArray 传递给详细视图?
【发布时间】:2013-05-24 21:29:16
【问题描述】:

我目前正在使用以下 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'

任何帮助将不胜感激!!!

【问题讨论】:

  • 儿童 = [infectionChildren objectForKey:@"ChildName"];日志是否为孩子打印正确的输出?
  • infectionChildren 是一个数组。但是你像字典一样使用它。这就是错误消息告诉您的内容。
  • 我的坏大家这是食物儿童不感染儿童(切换它)。但是是的 Dana0550,它可以正确打印出来,但是在我的根视图控制器中。它永远没有机会在详细视图中记录它,因为它崩溃了。
  • 阅读我们的嘴唇:消息是说您正在尝试在阵列上执行objectForKey。 (而且它不喜欢那样。)
  • 阅读 plist:外层是一个字典。其中的“食物”元素是一个数组。 “食物”的元素是字典。其中一个字典的“儿童”元素是一个数组。该数组包含字典。

标签: ios objective-c uitableview nsarray nsdictionary


【解决方案1】:

在我看来infectionChildrenNSArray(可能与[yourPlist objectForKey:@"Children"] 一起使用,尽管我在您发布的代码中没有看到)。如果infectionChildren 是一个NSDictionaries 数组,那么你不能只使用[infectionChildren objectForKey:@"ChildName"],因为NSArray 不会响应objectForKey。

您应该遍历您的数组以提取子名称,例如:

NSMutableArray *children = [[NSMutableArray alloc] initWithCapacity:[infectionChildren count]];
for (NSDictionary *childDictionary in infectionChildren) {
    [children addObject:[childDictionary objectForKey:@"ChildName"]];
}

【讨论】:

    【解决方案2】:

    这是因为您在 NSArray 上使用 NSDictionary 方法objectForKey: 用于字典,但你使用的infectionChildren 是一个数组。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      您收到该错误是因为您似乎正在尝试将“objectForKey”消息发送到 NSArray。

      根据您的 plist 设置和代码,HDViewController.foodChildren 是一个数组,所以我的问题是您如何将它传递给变量“infectionChildren”?

      您应该将 HDViewController.foodChildren NSArray 中的对象解析为

      中的 infectionChildren
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      
      NSDictionary *infectionChildren = [self.foodChildren objectAtIndex:indexPath.row]; 
      
      }
      

      【讨论】:

      • 嘿@iamraymondchen 哎呀!感染Children其实是foodChildren,我忘记在代码里改了。 foodChildren 也是一个 NSDictionary,所以 objectAtIndex: 不起作用:/
      猜你喜欢
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 2014-05-23
      • 2016-10-10
      相关资源
      最近更新 更多