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

Alfonso Lagrasta
Alfonso Lagrasta
7,551 Points

Sections + NSDictionary in Blog Reader

HI guys,

I'm studying the blog reader app in OBJ-C and I'm trying to implement 3 different sections along with NSDictionary to show titles and blog author in every section. The problem is it only shows the first title for each blogposts in each section and no author (the style 'Subtitle' in the storyboard is selected). My code is the following:

iOS

- (void)viewDidLoad
{
    [super viewDidLoad];



    NSDictionary *blogpost1 = [NSDictionary dictionaryWithObjectsAndKeys:@"Titol1",@"title",@"Alfonso Lagrasta",@"author",@"Titolo2",@"title2",@"Alfonso Lagrasta",@"author2",@"Titolo3",@"title3",@"Alfonso Lagrasta",@"author3",nil];



    NSDictionary *blogpost3 =[NSDictionary dictionaryWithObjectsAndKeys: @"Titol6",@"title",@"Alfonso Lagrasta",@"author",@"Titolo7",@"title2",@"Alfonso Lagrasta",@"author2",@"Titolo8",@"title3",@"Alfonso Lagrasta",@"author3",nil];

    NSDictionary *blogpost4 = [NSDictionary dictionaryWithObjectsAndKeys: @"Titol9",@"title",@"Alfonso Lagrasta",@"author",@"Titolo10",@"title2",@"Alfonso Lagrasta",@"author2",@"Titolo11",@"title3",@"Alfonso Lagrasta",@"author3",nil];




    self.titles = [NSArray arrayWithObjects:blogpost1, nil];




    self.Mobile = [NSArray arrayWithObjects:blogpost3, nil];


    self.Development = [NSArray arrayWithObjects:blogpost4, nil];



    self.sections = [NSArray arrayWithObjects:self.Mobile,self.Development,self.titles, nil];






}
Alfonso Lagrasta
Alfonso Lagrasta
7,551 Points
- (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 [self.sections count];
}

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

    return [[self.sections objectAtIndex:section] count];

   }



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


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


    NSDictionary *blogpost2 =[self.Mobile objectAtIndex:indexPath.row];
    NSDictionary *blogpost3 =[self.Development objectAtIndex:indexPath.row];
     NSDictionary *blogpost4 =[self.titles objectAtIndex:indexPath.row];



     if (indexPath.section == 0) {

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

    else if (indexPath.section == 1){

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

    else if (indexPath.section == 2){
        cell.textLabel.text = [blogpost4 valueForKey:@"title"];
        cell.detailTextLabel.text = [blogpost4 valueForKey:@"author"];
    }

    return cell;

}

Amit Bijlani

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi ,

In order for the detailTextLabel property to work you need to set the style of the cell which you can do using the below code, the new code is the IF statement which creates the UITableViewCell with a default style that inherits both label views instead of the one.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // ...

    return cell;
}
Alfonso Lagrasta
Alfonso Lagrasta
7,551 Points

Thank you Chris, I've added the code you wrote above but nothing happens, this is the implementation:

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


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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *blogposts1 =[self.Mobile objectAtIndex:indexPath.row];
    NSDictionary *blogposts2 =[self.Development objectAtIndex:indexPath.row];
    //NSDictionary *blogposts3 = [self.Design objectAtIndex:indexPath.row];//


     if (indexPath.section == 0) {

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

     else if (indexPath.section == 1){

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

    //else if (indexPath.section == 2){
        //cell.textLabel.text = [blogposts3 valueForKey:@"title"];
        //cell.detailTextLabel.text = [blogposts3 valueForKey:@"author"];
    //}

    return cell;

What's wrong with it? (Besides this when I run the program adding the NSDictionary variable *blogposts3 it gives me a run time error, that's why I made it as a notation).