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 Viewing a Web Page Opening a Web Page in Safari

Richard Lu
Richard Lu
20,185 Points

didDeselectRowAtIndexPath is not giving me the correct row number

This is what I have so far

- (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];
//    self.blogPosts = [dataDictionary objectForKey:@"posts"];

    self.blogPosts = [NSMutableArray array];
    NSArray *blogPostArray = [dataDictionary objectForKey:@"posts"];

    for(NSDictionary *bpDictionary in blogPostArray) {
        BlogPost *blogPost = [BlogPost blogPostWithTitle:
                              [bpDictionary objectForKey:@"title"]];
        blogPost.author = [bpDictionary objectForKey:@"author"];
        blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];
        blogPost.date = [bpDictionary objectForKey:@"date"];
        blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"url"]];
        [self.blogPosts addObject:blogPost];
    }



    [@"Avengers" isKindOfClass:[NSString class]];
    NSLog(@"%@",[@"Avengers" class]);



}

- (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
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 20)];
    self.tableView.tableHeaderView = headerView;

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

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

    cell.textLabel.text = blogPosts.title;
    cell.detailTextLabel.text = [[blogPosts.author stringByAppendingString:@" "] stringByAppendingString:[blogPosts formattedDate]];


    if([blogPosts.thumbnail isKindOfClass:[NSString class]]) {
        NSData *imageData = [NSData dataWithContentsOfURL:blogPosts.thumbnailURL];
        UIImage *image = [UIImage imageWithData:imageData];
        cell.imageView.image = image;
    }


    return cell;
}

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Row Selected: %d", indexPath.row);
}

The results are just random numbers How would I fix this?

Richard Lu
Richard Lu
20,185 Points

Nevermind I found out that the method name was deselect instead of select

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Common mistake but glad you figured it out.