Question

I am newbie for iOS programming. I'm trying to create an array of arrays from json for the drill down tableview. So I want to get this: blogsList --> blogPosts --> postDetailView. I've spent 2 days on it but still have no results. Help, please.

Here is my 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"}]}] }

Here is my code:

...
- (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 shows an array with dictionary with key "blogName" and "posts". Then I use method cellForRowAtIndexPath:

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

It gives me a list of blogNames in my first tableView. Then I use method didSelectRowAtIndexPath:

But here I can't get postsTitle to show posts list in next tableView when user select the blog. What am I doing wrong? Please help me!

Upd: I think dictionary with posts creates like an array and have no keys that I need ("desc", "title" and so on). I can't see mistakes in code for this suggestion.

Was it helpful?

Solution

You will have to insert the title in the dictionary like following

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

Now in your didSelectRowAtIndexPath get the post title like this

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

OTHER TIPS

in side the

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

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top