UITableView and UIViewcontroller as a subview

This drove me nuts for a few hours, but finally figured it out. I wanted to go ahead and share, since the web results I found when I was searching were only a few pieces here and there, and still didn't offer a final solution.

So the scenario I encountered was that I have a UIViewController that will sometimes host a subview (that is another viewcontroller) and it has a UITableView in it. The idea is that sometimes the user will tap a button, and it will add a subview of that UIViewController.

Originally I created a file that subclassed UITableViewController but I've recently found that I prefer to create UIViewControllers that have tableViews in them. They're easier to manage, and give you more control over the view. For example, you can add subviews a lot easier to a UIViewController than you can a UITableViewController.

The major issue I was having was that the UITableView did not resize properly when added as a subview. It would show some of the rows, but when the list became very long, it wouldn't let the user flick down to them. In general it was just being very strange and didn't seem to respond to any kind of frame setting.

What worked was that I created a new UIViewController (without a nib file) and implemented the following code in the loadView:

// Implement loadView to create a view hierarchy programmatically, without using a nib.

- (void)loadView

{

 

UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

contentView.autoresizesSubviews = YES;

contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

contentView.backgroundColor = [UIColor clearColor];

 

[self setView:contentView];

[contentView release];

 

tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 440) style:UITableViewStylePlain];

[tableView setAutoresizesSubviews:YES];

[tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];

 

 

[tableView setDataSource:self];

[tableView setDelegate:self];

 

[[self view] addSubview:tableView];

}

 

So in the loadView method, I create a UIView programmatically and set it to a full size frame. I then create a tableView and this is where I create the custom frame size that my app required. (I set it to be 440 instead of the full 480 for height). If you wanted, you could make this a variable that you initialize the UIViewController with.

If you're curious what the header file looks like, here it is:

@interface SelectPeopleViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

 

UITableView *tableView;

}

 

@property (nonatomic, retain) UITableView *tableView;

 

By doing this, I was finally able to add the UIViewController as a subview and have it be sized properly, and the tableview finally rendered the list properly.

Hope this info helps!

Print | posted on Wednesday, April 20, 2011 2:21 PM

Feedback

# re: UITableView and UIViewcontroller as a subview

left by web design southampton at 5/10/2011 12:56 AM Gravatar
It would show some of the rows, but when the list became very long, it wouldn't let the user flick down to them. In general it was just being very strange and didn't seem to respond to any kind of frame setting.

# re: UITableView and UIViewcontroller as a subview

left by Joshua Moody at 8/18/2011 4:01 PM Gravatar
Nicely done.

# re: UITableView and UIViewcontroller as a subview

left by James at 10/25/2011 2:32 AM Gravatar
Thank you! I also couldn't find the right information on how to do this. I tried for hours both through code and IB and couldn't get it right.

Thanks for the help.

# re: UITableView and UIViewcontroller as a subview

left by joe tiger at 1/10/2012 1:11 AM Gravatar
Hi
Is it possible to insert the tableView as a part of the ViewController and not take all the view ?
Regards
Joe

# re: UITableView and UIViewcontroller as a subview

left by alex at 3/26/2012 5:05 AM Gravatar
Thanks! )

# re: UITableView and UIViewcontroller as a subview

left by Diego at 6/23/2012 8:48 AM Gravatar
Excelent! i really thank you!

# re: UITableView and UIViewcontroller as a subview

left by Joev at 1/27/2013 4:29 PM Gravatar
Hey, thanks you just saved my good night sleep :)

# re: UITableView and UIViewcontroller as a subview

left by Tim at 8/22/2013 8:36 PM Gravatar
Hey thanks so much for that! Saved me some brain strain and hours of frustration!
Post A Comment
Title:
Name:
Email:
Comment:
Verification: