0

我是 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”等)。我看不到这个建议的代码错误。

4

2 回答 2

0

在 - 的里面

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

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

}
于 2012-06-19T08:52:16.400 回答
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"];
}
于 2012-06-19T08:54:52.413 回答