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!
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

Micah Elias
2,774 PointsHelp With Adding A Table View Controller Class
Hi! I am following the video called Help With Adding A Table View Controller Class. I did everything in the video and I downloaded the project files and compared my file to the Treehouse file and they were exactly the same. This is my code:
#import "MSTableViewController.h"
@interface MSTableViewController ()
@end
@implementation MSTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.titles = [NSArray arrayWithObjects:@"Hello World",@"Objective - C", @"Hello", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [self.titles count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
Can someone please fix my code?
4 Answers

John W
21,558 PointsRight now, you are telling the table view to generate many sections and zero row, i.e. nothing, for each section. Instead, you want 1 section (presumably) and number of rows according to [self.titles count]

Matthew Mascioni
20,444 PointsHi Micah,
Your code doesn't look like it has any errors. Could you share what problem the code is causing? Also, when hooking up a custom table view controller class, make sure two things happen:
In your Storyboard (assuming the project is using them), be sure to set the 'Custom Class' in the Identity Inspector (this can be found in the Utilities pane) to the custom class you've created (in your case, MSTableViewController)
Again, in your Storyboard file, where the table view controller is, click on the cell, and be sure to set the Reuse Identifier to "Cell" (as this is the value set in your custom class)
Hope this helps!

John W
21,558 PointsFor future questions, any code snippets longer than a few lines are best shared using Github gist
This allows other students and instructors to view the code easier than scrolling through the code within the forum markdown.

Micah Elias
2,774 PointsI fixed my code