user interface – How to resize UI Table View of a UI Table View Controller programmatically? – Education Career Blog

I subclassed UITableViewController and called it FeaturedGamesViewController. Ok, I also have a navigation controller to which I added this FeaturedGamesViewController as the root. All pretty standard stuff that you do in the App Delegate.

Also note that there is no NIB file created for FeaturedGamesViewController. Just subclassing UITableViewController and calling initWithStyle sets the style of the table and sets the dataSource and delegate and size automatically. Data Source and Delegate are obviously set to FeaturedGamesViewController.

- (id)init

// Call the superclass’s designated initializer
super initWithStyle:UITableViewStyleGrouped;


OK, You see that I have set the table size to “Grouped”. In Landscape view on my iPad it has about 20 pixels of space to the top, left and right (Sorry can’t post screen shot because I am new here and the system won’t let me until I have accumulated a certain number of points)

I DO NOT want that! This is Landscape so I expect it to fill up the all the space between the navigation bar and the tab bar below. What is worse is that I have faked a grid with a Custom UITableViewCell but the space to the left and right make it so that if you click on that space, the entire row is selected thus betraying the sense that this is a grid.

Now I figure I should resize the table view in viewDidLoad or something but I don’t know how. I cannot do initWithFrame because of potential memory leaks (and possibly resetting dataSource and delegate and autoresizeMask properties that were already set) so there must be a setter or something to reset the origin of the tableview to just beneath the Navigation bar and filling up the entire screen with size 1024X748. How do you do dynamically reset the size of the table view?

Then I got really frustrated and I decided to do it via a Nib file, that way I can set the the orientation to landscape and set simulated Navigation and Tab bars and fill the rest of the space with the table view. This worked! If you are curious how to create a table view with a Nib after you have subclassed UITableViewController WITHOUT a nib, here is how:

Go to the paragraph right before “Creating a Table View Programmatically”.

OK, when I did that, my landscape view of the “grid” looks filled up the entire space between the navigation bar at the top and the tab bar at the bottom just like I wanted.

I was fiddling with this some more and I found out that in my “non nib” version (the problematic one), I had set the table style to “grouped”. When I changed it to “plain”, it worked!!! But here is the thing though: In the nib version, “grouped” or “plain” gives the correct layout with the table occupying the whole space. So what gives?

Sorry for the long question but I guess what I am asking is:

1) How do you programmatically reset the size of the table view without introducing potential memory leaks or affecting other properties already set for you (ex: dataSource, delegate, autoResizeMask – these are set for you just because you subclassed UITableViewController)?

2) How do you do that for any view in general?

3) Why does “plain” style fill the layout as desired whereas “grouped” style gives the weird layout. Note that it this is not a problem in the Nib version.

Thanks for your patience!


Answer for (2), and hence for (1):

A UIView’s frame is in a local coordinate system of its superview. A common way to make a view fit its superview is

CGRect bounds = self superview bounds;
self setFrame:bounds;

You should do this inside layoutSubviews.

Leave a Comment