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

"What is NS Dictionary" Problem

Hi all, I'm having a problem with the NS Dictionary tutorial. Problem is this. I've followed the video's instructions, but got stuck on the part where you run the program in order to display three titles (at the 9:50 mark). When I run the program, only one title appears. Oddly the two rows below the title respond to being clicked. So clearly I've accomplished creating the three rows. But I've only populated one of them using NSDictionary and NSArray.

I've gone over the code numerous times to make certain that everything is exactly the way it should be. What am I missing? Here is my code in the TableViewController.m:

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

    NSDictionary *blogPost1 = [NSDictionary dictionaryWithObjectsAndKeys:@"The Missing Widget in Android",@"title", @"Ben Jakuben",@"author", nil];

    NSDictionary *blogPost2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Getting Started with iOS Development",@"Title", @"Amit Bijlani",@"Author", nil];

    NSDictionary *blogPost3 = [NSDictionary dictionaryWithObjectsAndKeys:@"Some other Title", @"Title", @"Some Other Guy", @"Author", nil];


    self.blogPosts = [NSArray arrayWithObjects:blogPost1, blogPost2, blogPost3, nil];

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

    cell.textLabel.text = [blogpost valueForKey:@"title"];

    return cell;

}

@end

3 Answers

your keys for the last two NSDictionary objects are @"Title" whereas the first one is @"title. that explains why only the first is being displayed since you call

[blogpost valueForKey:@"title"];
Chris McKnight
PLUS
Chris McKnight
Courses Plus Student 11,045 Points

The keys for you dictionaries aren't consistent. You have @"title" for the first one and @"Title" for the other two so you have 2 cells that are blank. You can tell they are blank because you can tap on them and highlight them. You should also be consistent with your author key as well. The code below will resolve your issue.

NSDictionary *blogPost1 = [NSDictionary dictionaryWithObjectsAndKeys:@"The Missing Widget in Android",@"title", @"Ben Jakuben",@"author", nil];

    NSDictionary *blogPost2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Getting Started with iOS Development",@"title", @"Amit Bijlani",@"author", nil];

    NSDictionary *blogPost3 = [NSDictionary dictionaryWithObjectsAndKeys:@"Some other Title", @"title", @"Some Other Guy", @"author", nil];

Thank you both for the prompt responses - that fixed it. I figured the issue was something small and "minor" - although I am quickly learning there is no such thing as a minor mistake in programming. Those small mistakes will kill your program just as easily as the big ones (and are far more insidious because they're harder to identify).

Thanks again!

yeah that one took me a while to catch. I looked at all your code and everything looked good...then I saw it