Alex Rivera | Logout

adding a UITableView programmatically to a UIViewController

Asked 2011-08-22T20:06:33.117
13

I'm loading a UIViewController into one of my Nav controller's hierarchies, which will contain some text and some images. At the bottom, I will want to create a expandable and collapsable tableview.

First off, is this idea possible? If it is, how do I add it and where do I place the data source and delegate methods?

Can I just make a separate subclass of the TableViewController and then add it to my ViewController as a subview?

Edit
Report

2 Answers

15
/************************************************/
/************* MyCustomController.m *************/
/************************************************/

@interface MyCustomController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MyCustomController

- (id)initWithNibName:(NSString*)nibName bundle:(NSString*)bundleName
{
   self = [super initWitNibName:nibName bundle:bundleName];
   if (self)
   {
       self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
       tableView.datasource = self; 
       tableView.delegate = self;
       [self.view addSubview:self.tableView];
   }

   return self;
}

#pragma mark - UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // return number of rows
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // return cell
}

#pragma mark - UITableViewDelegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // handle table view selection
}

@end
answered 2012-07-12T12:11:52.573
4

Just remember that a UITableViewController is a subclass of UIViewController only with the tableview set as the controller's view.

So yes definitely possible and used quite frequently when you want to have a tableview but also other custom UI elements which prevent you from using the UITableViewController.

I'd normally choose to add it to my view controller's view in either its initialisation method or viewDidLoad method. This will vary based on whether you're creating your views from a NIB or entirely programatically.

In case of NIBs:

- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)bundleName
{
   if ((self = [super initWitNibName:nibName bundle:bundleName]))
   {
       self.theTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewWhateverStyleYouWantHere];
       theTableView.dataSource = self, theTableView.delegate = self;
       [self.view addSubview:theTableView];
       [theTableView release];
   }
}

And then you can set the frame of your tableview in your viewDidLoad method.

I'd personally prefer to do the whole thing in interface builder as you'd achieve the same result with way less code to maintain.

answered 2011-08-22T21:26:26.293

Your Answer