Geeks With Blogs
Create Fun Things - Samer's Developer Blog Development Blog for C#, .NET and Obj-C

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!

Posted on Wednesday, April 20, 2011 2:21 PM Quick Tips | Back to top


Comments on this post: UITableView and UIViewcontroller as a subview

# re: UITableView and UIViewcontroller as a subview
Requesting 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.
Left by web design southampton on May 10, 2011 12:56 AM

# re: UITableView and UIViewcontroller as a subview
Requesting Gravatar...
Nicely done.
Left by Joshua Moody on Aug 18, 2011 4:01 PM

# re: UITableView and UIViewcontroller as a subview
Requesting 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.
Left by James on Oct 25, 2011 2:32 AM

# re: UITableView and UIViewcontroller as a subview
Requesting Gravatar...
Hi
Is it possible to insert the tableView as a part of the ViewController and not take all the view ?
Regards
Joe
Left by joe tiger on Jan 10, 2012 1:11 AM

# re: UITableView and UIViewcontroller as a subview
Requesting Gravatar...
Thanks! )
Left by alex on Mar 26, 2012 5:05 AM

# re: UITableView and UIViewcontroller as a subview
Requesting Gravatar...
Excelent! i really thank you!
Left by Diego on Jun 23, 2012 8:48 AM

# re: UITableView and UIViewcontroller as a subview
Requesting Gravatar...
Hey, thanks you just saved my good night sleep :)
Left by Joev on Jan 27, 2013 4:29 PM

# re: UITableView and UIViewcontroller as a subview
Requesting Gravatar...
Hey thanks so much for that! Saved me some brain strain and hours of frustration!
Left by Tim on Aug 22, 2013 8:36 PM

# re: UITableView and UIViewcontroller as a subview
Requesting Gravatar...
I have a doubt in my project i have to made one table view in this 5 fields are there.those are home,about us,property,blog,Useful links.in this views again aboutus field have 4 sub fields,property have 3 sub fields,blog have 2 sub fields please tell me source code
Left by priya on Jan 03, 2015 12:13 AM

Your comment:
 (will show your gravatar)


Copyright © samerpaul | Powered by: GeeksWithBlogs.net | Join free