我是 iOS 编程的新手。我正在尝试从 json 创建一个数组数组以用于向下钻取表视图。所以我想得到这个:blogsList --> blogPosts --> postDetailView。我已经花了2天的时间,但仍然没有结果。请帮忙。
这是我的json:
{ "blogs": [{"ID":8,"Title":"ringroads","Name":"utf8 coded name
string","Logotype":"..\/images\/gup_logos\/ringroads_logo_mini.jpg","posts":
[{"ID":38,"URLTitle":"remont_dorog_v_moskve","title":"utf8 coded title string","Desc":"utf8 coded description
string","0":"","Preview":"remont_dorog_v_moskve.jpg","create_time":"2012-05-22
17:40:11"}]},{"ID":9,"Title":"gormost","Name":"utf8 coded name
string","Logotype":"..\/images\/gup_logos\/gormost_logo_mini.jpg","posts":
[{"ID":35,"URLTitle":"rabochie_budni_uchastka_gormost__fontany","title":"utf8 coded
title string","Desc":"utf8 coded description
string.","0":"","Preview":"rabochie_budni_uchastka_gormost__fontany.jpg","create_time":"2012
-05-18 10:17:32"}]},{"ID":10,"Title":"unecomoscow","Name":"utf8 coded name
string","Logotype":"..\/images\/gup_logos\/unecomoscow_logo_mini.jpg","posts":
[{"ID":52,"URLTitle":"documentooborot","title":"utf8 coded title string","Desc":"utf8
coded description string.","0":"","Preview":"documentooborot.jpg","create_time":"2012-
06-05 14:02:23"},{"ID":49,"URLTitle":"grebnoy_kanal","title":"utf8 coded title
string","Desc":"utf8 coded description
string.","0":"","Preview":"grebnoy_kanal.jpg","create_time":"2012-05-31 14:37:08"},
{"ID":46,"URLTitle":"itogi_ozp","title":"utf8 coded title string.","Desc":"utf8 coded
description string.","0":"","Preview":"itogi_ozp.jpg","create_time":"2012-05-30
10:13:11"}]}] }
这是我的代码:
...
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseStringBlogs = [[NSString alloc] initWithData:responseDataBlogs encoding:NSUTF8StringEncoding];
self.responseDataBlogs = nil;
NSDictionary *results = [responseStringBlogs JSONValue];
NSArray *blogs = [results objectForKey:@"blogs"];
int i;
NSMutableDictionary *blogsDict = [[NSMutableDictionary alloc] init];
for(i = 0; i < [blogs count]; i++){
NSDictionary *tmpBlog = [blogs objectAtIndex:i];
NSString *blogName = [tmpBlog objectForKey:@"Name"];
NSDictionary *blogPosts = [tmpBlog objectForKey:@"posts"];
//NSDictionary *blog = [NSDictionary dictionaryWithObjectsAndKeys:blogName, @"blogName", blogPosts, @"posts", nil];
NSMutableDictionary *blog = [[NSMutableDictionary alloc] init];
[blog setObject:blogName forKey:@"blogName"];
[blog setObject:blogPosts forKey:@"posts"];
//[blog setObject:urlTitle forKey:@"urlTitle"];
blogsDict = blog;
[arrayForTable addObject:blogsDict];
}
NSLog(@"blogsArr == %@", arrayForTable);
NSLog 显示了一个带有字典的数组,其键为“blogName”和“posts”。然后我使用方法 cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
NSDictionary *dict = [arrayForTable objectAtIndex:indexPath.row];
cell.textLabel.text = [dict objectForKey:@"blogName"];
}
它在我的第一个 tableView 中为我提供了 blogNames 列表。然后我使用方法 didSelectRowAtIndexPath:
但是在这里,当用户选择博客时,我无法让 postTitle 在下一个 tableView 中显示帖子列表。我究竟做错了什么?请帮我!
Upd:我认为带有帖子的字典像数组一样创建并且没有我需要的键(“desc”、“title”等)。我看不到这个建议的代码错误。