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

JSON Data not being displayed from lesson

Im curious as to why the treehouse blog JSON data is not showing in my table. The previous lessons hardcoded values were being displayed fine, but when I changed the data source to be the treehouse blog data, it stopped displaying. I know the URL I have is correct b/c I am logging the NSDictionary contents and it is pulling all the treehouse blog posts as expected.

Any suggestions?

2 Answers

Stone Preston
Stone Preston
42,016 Points

in your cellForRowAtIndexPath method, try using objectForKey: @"author" and objectForKey@"title" instead of valueForKey:@"Author" etc. the key probably starts with lowercase, not uppercase. Since they are in a dictionary you want to use objectForKey as well.

// Configure the cell...
    cell.textLabel.text = [blogPost objectForKey:@"title"];
    cell.detailTextLabel.text= [blogPost objectForKey:@"author"];

That did the trick! So, I see I need to pay attention to case sensitivity, but why objectForKey, rather than Value?

Stone Preston
Stone Preston
42,016 Points

see this stack overflow post for the difference between the 2. Basically objectForKey is a dictionary method. valueForKey is a method used for KVC

Stone Preston
Stone Preston
42,016 Points

are you reloading your tableView after obtaining the data? can you post the code that pulls the data from the URL? actually go ahead and post your whole view controller

//
//  TableViewController.m
//  newBlogReader
//
//  Created by Christian Bryant on 9/13/14.
//  Copyright (c) 2014 Christian Bryant. All rights reserved.
//

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

    //get url
    NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary"];
    //get data from the url
    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];


    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];


    NSLog(@"%@",dataDictionary);
    self.blogPosts = [dataDictionary objectForKey:@"posts"];


}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

    // Configure the cell...
    cell.textLabel.text = [blogPost valueForKey:@"Title"];
    cell.detailTextLabel.text= [blogPost valueForKey:@"Author"];
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end