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

Cannot get the 3 sections to display in simulator

iOS Development > Build a Blog Reader iPhone App > Rebuilding from Scratch extra credit

Here's my code in the tableviewcontroller implementation file.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.design = [NSArray arrayWithObjects:@"css",
                   @"html",
                   @"Javascript", nil];

    self.development = [NSArray arrayWithObjects:@"php",
                        @"python",
                        @"ruby", nil];

  self.mobile = [NSArray arrayWithObjects:@"java",
                 @"Objective C", nil];
  self.coding = [NSArray arrayWithObjects:design,
                 development,
                 mobile, nil];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

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

}

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

      // Configure the cell...
  cell.textLabel.text = [[self.coding objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

What did i do wrong or miss? Are they solutions to these extra credit exercise for our reference?

This is solved.

5 Answers

I was struggling with this, and finally figured it out. This post helped me solve it, so I'll post my working code here for anyone else who's struggling with it.

This doesn't put any dividers between the sections, nor does it add titles for the sections, but it does seem to work.

//in the .h file:
@property (strong, nonatomic) NSArray *designTitles;
@property (strong, nonatomic) NSArray *developmentTitles;
@property (strong, nonatomic) NSArray *mobileTitles;
@property (strong, nonatomic) NSArray *sections;


// in the .m file

// Separated the titles up into their own arrays, and created an array to hold the arrays

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.designTitles = [NSArray arrayWithObjects:
                           @"Whitespace in Web Design: What It Is and Why You Should Use It",
                           @"Teacher Spotlight: Zac Gordon",
                           @"Do You Love What You Do?", nil];

    self.developmentTitles = [NSArray arrayWithObjects:
                           @"Getting started with WordPress",
                           @"A Guide to Becoming the Smartest Developer on the Planet",
                           @"Applying Normalize.css Reset - Quick Tip",
                           @"How I Wrote a Book in 3 Days", nil];

    self.mobileTitles = [NSArray arrayWithObjects:
                           @"Adaptive Images and Responsive SVGs - Treehouse Show Episode 15",
                           @"Productivity is About Constraints and Concentration",
                           @"Responsive Techniques, JavaScript MVC Frameworks, Firefox 16 | Treehouse Show Episode 14", nil];

    self.sections = [NSArray arrayWithObjects: self.designTitles, self.developmentTitles, self.mobileTitles, nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Get the number of sections by counting the number in the sections array.
    return [self.sections count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [[self.sections objectAtIndex:section] count];
}

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

    if (indexPath.section == 0){
        cell.textLabel.text = [self.designTitles objectAtIndex:indexPath.row];
    }
    else if (indexPath.section == 1){
        cell.textLabel.text = [self.developmentTitles objectAtIndex:indexPath.row];
    }
    else if (indexPath.section == 2){
        cell.textLabel.text = [self.mobileTitles objectAtIndex:indexPath.row];
    }

   // cell.textLabel.text = [self objectAtIndex:indexPath.row];

    return cell;
}
Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Glad you solved it! Feel free to share your answer here if you like.

It still doesn't seem to be working for me. Even if I literally copy and paste the code above that is said to work my view won't display section names and instead just displays all 3 array datas together. Is it cause I'm using Xcode 5 and things are a bit outdated? or do I have to make some changes to the story board to include sections in the viewController?

I was trying to figure this out too and the above posts helped me. I dived back into Google to figure out Titles and I found a switch example of what I wanted and changed it to an if statement with a bit of trial and error... here is my code for adding Titles to the TableView.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *sectionName;
    if (section == 0) {
        sectionName = @"Design";
    }
    else if (section == 1) {
        sectionName = @"Development";
    }
    else if (section == 2) {
        sectionName = @"Mobile";
    }
    return sectionName;
}

I don't know whether this is good practice or not, but it occurred to me that I could make the code less 'brittle'/more future proof by indexing the self.titles array with the indexPath.section integer value instead of just accessing self.designTitles etc in each If Else branch.

My code still works but I'd appreciate feedback as to whether this is a good idea, or one that could lead to problems, as I'm still learning the nuances of this kind of coding.

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

    // Configure the cell...

//    cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];

    if (indexPath.section == 0) {
        cell.textLabel.text = [self.titles[indexPath.section] objectAtIndex:indexPath.row];
    } else if (indexPath.section == 1) {
        cell.textLabel.text = [self.titles[indexPath.section] objectAtIndex:indexPath.row];
    } else if (indexPath.section == 2) {
        cell.textLabel.text = [self.titles[indexPath.section] objectAtIndex:indexPath.row];
    }

    return cell;
}