【发布时间】: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