Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Blog Reader iPhone App Getting Data from the Web Downloading and Parsing JSON Data

Dawid Cedrych
Dawid Cedrych
15,108 Points

Empty rows

I did everything just like it was shown on video. Data are downloaded properly, I see it in console. Nevertheless when running, application in simulator displays empty rows, any of you encountered such problem?

6 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

I'm pretty sure it's supposed to be:

self.blogPosts = [dataDictionary objectForKey:@"posts"];

and not

self.blogPosts = [dataDictionary objectForKey:@"id"];

In your viewDidLoad. If you want to now why, just look at the JSON:

"posts" is a big array containing dictionaries - which is exactly what you want :)

Thomas Nilsen
Thomas Nilsen
14,957 Points

Chances are you forgot something - Do you mind posing your code? :)

Dawid Cedrych
Dawid Cedrych
15,108 Points
#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *blogURL=[NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];
    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
    NSError *error = nil;
    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    NSLog(@"%@", dataDictionary);
    self.blogPosts = [dataDictionary objectForKey:@"id"];



//    NSDictionary *BlogPost1 = [NSDictionary dictionaryWithObjectsAndKeys:@"a",@"b",@"c",@"d", nil];
//    NSDictionary *BlogPost2 = [NSDictionary dictionaryWithObjectsAndKeys:@"e",@"b",@"g",@"d", nil];
//    NSDictionary *BlogPost3 = [NSDictionary dictionaryWithObjectsAndKeys:@"k",@"b",@"m",@"d", nil];
//    self.blogPosts = [NSArray arrayWithObjects:BlogPost1,BlogPost2,BlogPost3,nil];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}
//tutaj programujemy ile kolumn
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return self.blogPosts.count;
}

//tutaj wyswietlanie programuejmy
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier forIndexPath:indexPath];
    NSDictionary *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

    cell.textLabel.text = [blogPost valueForKey:@"title"];
    cell.detailTextLabel.text = [blogPost valueForKey:@"author"];

    return cell;
}
@end
Thomas Nilsen
Thomas Nilsen
14,957 Points

Edit - you did it - never mind - I'll have a look now :)

Dawid Cedrych
Dawid Cedrych
15,108 Points

Ha, thanks! how could I miss such simple issue!:)

Dawid Cedrych
Dawid Cedrych
15,108 Points

Ha, thanks! how could I miss such simple issue!:)