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

James Goodin
James Goodin
2,483 Points

Success - Challenge - Rebuild TableView Blog Reader from Scratch - Grouped

I took the extra credit challenge in Amit's Blog Reader series end of 'Rebuilding from Scratch'. I successfully got to work this evening after about 3 evenings of scratching my way to a solution. I kept getting close but was fighting the 'index out of bounds'. Tonight I got it to work after getting the numberOfRowsInSection and TableViewCell methods switch and if controls set up right. Sharing for those interested.

From Amit's directive Extra Credit Table View Sections

Create sections in your table view to categorize blog posts into Design, Development and Mobile.

Hint: You can create three separate arrays and modify the method numberOfSectionsInTableView to return the number three. In your cellForRowAtIndexPath method check indexPath.section and write an if statement or a switch statement to return text from the appropriate array.

Here is my main TableViewController code.

// // TableTableViewController.m // BlogReaderRaw // // Created by Admin on 3/20/15. // Copyright (c) 2015 Wood and Wire Ware. All rights reserved. //

import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

  • (void)viewDidLoad { [super viewDidLoad]; // three title arrays self.titlesDesign = [NSArray arrayWithObjects:@"The Missing Widget in the Android SDK: Smart Image View", @"Getting started with IOS Development", @"An Interview with Shay Howe", @"Treehouse Friends: Paul Irish", @"Getting a job in Web Design and Development", @"Treehouse Show Episode 13", @"LLJS, Navicons and Framework Flights", nil]; self.titlesDev = [NSArray arrayWithObjects:@"Writing Your Way to C#", @"How Swift!", @"What Became of Dr Dobbs?", @"C++, a journey", nil]; self.titlesMobile = [NSArray arrayWithObjects:@"The World within 320x480 pixels", @"Life in the Sandbox", @"Andwho...", nil]; // section header titles array
    self.sectionTitles = [NSArray arrayWithObjects:@"Design", @"Development", @"Mobile", nil]; }

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch (section) { case 0: return [self.titlesDesign count]; break; case 1: return [self.titlesDev count]; break; case 2: return [self.titlesMobile count]; default: break; } return section; }

  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *name;

    switch (section) { case 0: name = [self.sectionTitles objectAtIndex:section]; break; case 1: name = [self.sectionTitles objectAtIndex:section]; break; case 2: name = [self.sectionTitles objectAtIndex:section]; break; default: break; } return name; }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; } if (indexPath.section == 0) { NSString *design = [self.titlesDesign objectAtIndex:indexPath.row]; cell.textLabel.text = design; } if (indexPath.section == 1) { NSString *dev = [self.titlesDev objectAtIndex:indexPath.row]; cell.textLabel.text = dev; } if (indexPath.section == 2) { NSString *mobile = [self.titlesMobile objectAtIndex:indexPath.row]; cell.textLabel.text = mobile; }

    return cell; }

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end