从json创建数组以深入查看tableview

时间:2012-06-19 08:44:40

标签: iphone json uitableview drilldown

我是iOS编程的新手。 我试图从json创建一个数组数组,用于深入查看。所以我想得到这个:blogsList - > blogPosts - > postDetailView。我已经花了两天时间,但仍然没有结果。求助。

这是我的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显示一个带字典的数组,其中包含键&#34; blogName&#34;和&#34;帖子&#34;。然后我使用方法cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
NSDictionary *dict = [arrayForTable objectAtIndex:indexPath.row];
cell.textLabel.text = [dict objectForKey:@"blogName"];
}

它在我的第一个tableView中给出了一个blogNames列表。然后我使用方法didSelectRowAtIndexPath:

但是在这里,当用户选择博客时,我无法获得postsTitle来显示下一个表格中的帖子列表。 我究竟做错了什么?请帮帮我!

更新:我认为带有帖子的字典会像数组一样创建并且没有我需要的密钥(&#34; desc&#34;,&#34; title&#34;依此类推)。我无法在代码中看到这个建议的错误。

2 个答案:

答案 0 :(得分:0)

一边

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSDictionary *dict = [arrayForTable objectAtIndex:indexPath.row];
NSString *blogName = [dict objectForKey:@"blogName"];

}

答案 1 :(得分:0)

您必须在字典中插入标题,如下所示

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