Adding a TableView to a View in Xcode 4.2

January 21st, 2012

Sometimes you need something else to be shown on the screen besides just a TableView, maybe you want a search bar at the top or a ToolBar or a bunch of other elements. In this case using a UITableViewController as your view won’t do it. This is a simple guide of one way to solve this (I’m sure there might be others).

I’m using Xcode 4.2 and storyboards on this one although it doesn’t really make much difference in this case, working with xib files would be very similar.

First, for the view instead of using a UITableViewController use a UIViewController, drag it to your storyboard (hook it up with any existing scenes you might already have) and then find the TableView object in your library and drag it inside your view (be careful not to confuse it with the UITableViewController).

TableView Object

Now create a new file of type UIViewController subclass and call it whatever suits your app (I’ll call it DetailViewController for the purposes of this guide) and in your storyboard select the new view you created with the TableView inside and in the “Identity Inspector” set its class to DetailViewController by selecting it from the droplist.

Identity Inspector

Now since you want to control your TableView from within the view you need to implement a couple delegates so open up your DetailViewController.h file and change the interface line to:

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

Next you’ll want to hook up your TableView to your view by creating an IBOutlet for it, you can do this by going to the storyboard, making sure you have “show the Assistant editor” with DetailViewController.h open and crtl+click and drag from the TableView to your code and call it myTable (something else if you want).

Your DetailViewController.h file should be looking similar to this:

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
 
@property (strong, nonatomic) IBOutlet UITableView *myTable;
 
@end

Now if you did this like I said from dragging it in the storyboard you should also have a line like this in your DetailViewController.m file:

@synthesize myTable;

If not then type it now. If you try to build now it should succeed but you’ll have a few warnings because of protocol methods missing in the implementation. This means that since your view is your TableView’s delegate it is responsible for implementing its protocol and there are lots of methods in it but only a few are really needed for now. In your DetailViewController.m put in the following methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
 
    // configure your cell here...
 
    return cell;
}

You might also want to implement other delegate methods like – (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath but that’s beyond the scope of this guide.

The last step is to hook up your view as the TableView’s delegate and data source. To do this open the storyboard file, select your TableView and go to your “Connections Inspector” and drag from dataSource and delegate to your ViewController.

Connections Inspector

That’s it, you might want to customize your TableView, its cells and put in the other elements into the view now, have fun with it.

3 Comments to “Adding a TableView to a View in Xcode 4.2”.

  1. Franmoga on January 24, 2012 1:03 pm

    Really usefull. Thanks!

  2. Daniel Burke on February 24, 2012 7:02 pm

    Hi Hugo,

    First of all, thanks for putting this together. This is exactly what I need…however…I’m getting this error when I run the project with just the one UIViewController and the UITableView in it:

    [UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

    Then it terminates the app. I also tried simply adding this process to another app I’m working on with the same result.

    What could I be doing wrong?

    Thanks in advance.

  3. Daniel Burke on February 24, 2012 7:23 pm

    Figured it out…I had my dataSource and delegate hooked to the View, not the ViewController :)

    Thanks again


Leave a Comment

Name (required)

Email (required)

Website


Speak your mind