从 json 创建数组以制作向下钻取表视图



我是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"];
}

它在我的第一个表视图中为我提供了一个博客名称列表。然后我使用方法didSelectRowAtIndexPath:

但是在这里,当用户选择博客时,我无法获得帖子标题以在下一个表视图中显示帖子列表。我做错了什么?请帮帮我!

Upd:我认为带有帖子的字典就像一个数组,并且没有我需要的键("desc","title"等)。我看不到此建议的代码错误。

您必须在

字典中插入标题,如下所示

for(i = 0; i < [blogs count]; i++){
    NSDictionary *tmpBlog = [blogs objectAtIndex:i];
    NSString *blogName = [tmpBlog objectForKey:@"Name"];
    NSDictionary *blogPosts = [tmpBlog objectForKey:@"posts"];
    //Here get the post title too
    NSString *postTitle = [tmpBlog objectForKey:@"Title"];
    //NSDictionary *blog = [NSDictionary dictionaryWithObjectsAndKeys:blogName, @"blogName", blogPosts, @"posts", nil];
    NSMutableDictionary *blog = [[NSMutableDictionary alloc] init];
    [blog setObject:blogName forKey:@"blogName"];
    [blog setObject:blogPosts forKey:@"posts"];
    //Set title in dictionary too
    [blog setObject:postTitle forKey:@"postTitle"];
    //[blog setObject:urlTitle forKey:@"urlTitle"];
    blogsDict = blog;
    [arrayForTable addObject:blogsDict];
}

现在在你的didSelectRowAtIndexPath得到这样的帖子标题

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [arrayForTable objectAtIndex:indexPath.row];
    //Get the post title
    NSString *postTitle = [dict objectForKey:@"postTitle"];
}

在侧面

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dict = [arrayForTable objectAtIndex:indexPath.row];
NSString *blogName = [dict objectForKey:@"blogName"];
}

最新更新