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 Rebuilding from Scratch Model-View-Controller

Separate sections

PS: Sorry for the messed up code in this post, the whole tagging thing is confusing !!

So i did the extra credit assignment and made my table contain 2 sections using the IF method. ( used some help from google ) but now they look like as if they still one section. So how can i separate them ? here is my code:

   - (void)viewDidLoad
  {
    [super viewDidLoad];

    self.designTitles = [NSArray arrayWithObjects:@"Getting started with WordPress",       @"Whitespace in Web Design: What It Is and Why You Should Use It",
                   @"Adaptive Images and Responsive SVGs - Treehouse Show Episode 15",
                   @"Productivity is About Constraints and Concentration",
                   @"Teacher Spotlight: Zac Gordon",
                   @"Do You Love What You Do?",
                   @"Applying Normalize.css Reset - Quick Tip",

                    nil];

     self.developmentTitles =[NSArray arrayWithObjects:@"A Guide to Becoming the Smartest Developer on the Planet",@"Responsive      Techniques, JavaScript MVC Frameworks, Firefox 16 | Treehouse Show Episode 14",   @"How I Wrote a Book in 3 Days", nil];
    }

   - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
   }
   #pragma mark - Table view data source
   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
   {
   #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 2;
   }
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   {
    #warning Incomplete method implementation.
    // Return the number of rows in the section.

    if (section == 0) {
        return [self.designTitles count];
    }
    else{
        return [self.developmentTitles count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        // Configure the cell...

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

    return cell;
}

1 Answer

Aman Tohan
Aman Tohan
14,822 Points

I don't know if the OP (Original Poster) is still around but here's one way to do this...

To separate the titles, titleForHeaderInSection: method can be added to the TableViewController.m. Here is the code to do this based on original post.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return (section == 0) ? @"Design" : @"Development";
}

This will organize the titles by sections visually with the names provided in the method.

Based on the current stage in the course and the original code in this post, this should get the job done. However, there are other ways to setup the whole app in which the use of if statements and switch statements may not be required altogether. One of such ways is to make use of NSDictionary class. Though it may not be practically used in this way, to keep the titles organized/categorized by sections, the sections can be setup as keys and the titles can be setup in arrays as values.