我目前正在使用以下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'
任何帮助将不胜感激!!!
答案 0 :(得分:0)
我认为infectionChildren
是NSArray
(可能是[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"]];
}
答案 1 :(得分: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];
}
答案 2 :(得分:0)
这是因为你
希望它对你有所帮助。