0

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.

pash3r
  • 141
  • 2
  • 13

2 Answers2

0

in side the

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

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

}
Senthilkumar
  • 2,471
  • 4
  • 30
  • 50
  • Inside the didselectrowatindexpath NSArray *arrblogName = [dict objectForKey:@"blogName"]; NSString *blogName = [arrblogName objectAtIndex:2]; – Senthilkumar Jun 19 '12 at 09:16
  • just tried it -- it gives me an error. Because key "blogName" has only name of the blog. So I can not receive posts from it. – pash3r Jun 19 '12 at 09:19
  • in your connection did finish load function NSDictionary *blogPosts = [tmpBlog objectForKey:@"posts"]; instead of NSDictionary put NSArray – Senthilkumar Jun 19 '12 at 09:21
  • it crashes again when I select row – pash3r Jun 19 '12 at 09:25
  • can you post me what output shown if you NSLog(@"poststring== %@",[dict objectForKey:@"blogName"]); in didfinish function.omit past comment operation – Senthilkumar Jun 19 '12 at 09:28
  • 2012-06-19 13:38:17.180 testBlogs[7764:f803] postString == МГУП «Мосводоканал» So it is only name of the blog. – pash3r Jun 19 '12 at 09:39
  • hey put "posts" instead of the "blogName" in nslog – Senthilkumar Jun 19 '12 at 09:42
  • Inside the didselectrowatindexpath NSArray *arrblogName = [dict objectForKey:@"posts"]; NSString *blogName = [arrblogName objectAtIndex:2]; – Senthilkumar Jun 19 '12 at 09:45
  • postString == ( { 0 = ""; Desc = "utf8 coded"; ID = 65; Preview = "mer_moskvy_.jpg"; URLTitle = "mer_moskv"; "create_time" = "2012-06-18 17:57:05"; title = "utf8 coded title"; }, { 0 = ""; Desc = "utf8 coded"; ID = 30; Preview = "ito.jpg"; URLTitle = "itogi"; "create_time" = "2012-04-25 18:11:23"; title = "utf8 coded title"; } ) .And nsstring *blogname=[arrblogname objectatindex:2]; gives error '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' – pash3r Jun 19 '12 at 09:53
  • Inside the didselectrowatindexpath NSArray *arrblogName = [dict objectForKey:@"posts"]; NSString *blogName = [arrblogName objectAtIndex:2]; change the objectAtIndex value according to your rquired field name. mind it array start from 0. – Senthilkumar Jun 19 '12 at 09:57
  • it returns 1 post with id, title, created_time, preview and urltitle. I need all posts and only "title" from it so it'll make possible for me to create new table with posts. – pash3r Jun 19 '12 at 10:05
0

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"];
}
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • I've tried to insert postTitle before but it gives an error: -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x689a0d0. Now I've just do the same that you wrote -- same problem. I think there is some mistakes when I create an array for uitable. – pash3r Jun 19 '12 at 09:03
  • no lines is highlighted. I have message in console only 2012-06-19 13:00:08.343 testBlogs[7426:f803] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x689a0d0 2012-06-19 13:00:08.344 testBlogs[7426:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x689a0d0' *** First throw call stack: – pash3r Jun 19 '12 at 09:10
  • does it happen when you press on the cell? – Omar Abdelhafith Jun 19 '12 at 09:18
  • instead of NSDictionary *blogPosts = [tmpBlog objectForKey:@"posts"]; use NSDictionary *blogPosts = [[tmpBlog objectForKey:@"posts"] objectAtIndex0]; – Omar Abdelhafith Jun 19 '12 at 09:27
  • *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' – pash3r Jun 19 '12 at 09:34