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 Adding a Table View Controller Class

Luke Schoen
Luke Schoen
3,317 Points

Lesson learnt on the Importance of selecting the correct parent class for your custom class to inherit from

I am using iOS SDK 7.1. When I compare the contents of TableViewController.m from the downloaded Project File with the custom class that I created TableViewController.m (following the instructions in the video), the following differences are apparent:

  • my code is pre-populated with a 'initWithNibName' method, which appears to have replaced the 'initWithStyle' method shown the video
  • my code is pre-populated with a '#pragma mark - Navigation' section with a method 'prepareForSegue', whilst the video does not have this section and method and instead has methods under '#pragma mark - Table view data source' and under '#pragma mark - Table view delegate' (which my code did not come pre-populated with)

What could be the cause of these differences?

In order to test the app I copied from the downloaded TableViewController.m Project File to my TableViewController.m file all the methods under '#pragma mark - Table view data source' and under '#pragma mark - Table view delegate'. I then attempt to follow the instructions to connect the TableViewController custom class to the TableViewController in the Storyboard by selecting the TableViewController View and then going to Identity Inspector to add 'TableViewController' as the custom class... but it wouldn't let any text that I typed in stay permanently... Anyway, in the process of posting this message I discovered WHY it wouldn't let me. It was because when I created the custom class I accidently made it a 'Subclass of:' UIViewController, instead of UITableViewController. To fix the problem and allow me to connect the custom class, I simply edited by TableViewController.h as follows: @interface TableViewController : UITableViewController

Now when I compile the code the Simulator shows the blog titles instead of just horizontal lines :-)

BUT... whilst the code now works, I still don't have an answer to my original question as to why there may be differences in pre-populated code of my custom TableViewController class. Well.. I decided to see what would happen if I created another custom class and actually gave it the correct 'Subclass of:' UITableViewController upfront, and wow, I solved my own problem!! When I chose the correct one, the custom class that was created inherited all those methods that were in the Project Files, highlighting that the reason why I had encountered the difference earlier was because I had created a custom class that was a subclass of UIViewController instead of UITableViewController, which obviously has different methods (which my class inherits)!!