views:

632

answers:

3

Hello everyone

I have a UIView (created in IB) with a grouped UITableView as a subview. Below this table view is a UIButton. The XIB containing the view will be loaded by a few different viewcontrollers, and so the contents of the table view can vary between one and four cells.

Here's what I want to achieve: when the view loads, the height of the tableview (tableView.frame.size.height) should be adjusted depending on the number of cells, and the button should be placed just beneath the table view.

Can this be done? Could it somehow be done if the view is created programmatically?

Thanks in advance

Edit: Pxl's suggestion was just what I was looking for. A while later, the need arose to have more than just a button below the table view - this was accomplished by creating a separate view containing everything I needed, and implementing the tableView:viewForFooterInSection: and tableView:heightForFooterInSection: functions.

A note for those of you trying to do the same thing: the tableview has to be programmatically created if you want different heights for the footers, or footers for only some of the sections. This is because the footer height set in IB will override the one returned from the tableView:heightForFooterInSection: function.

+1  A: 

if there are only a handful of rows, may i suggest that you create a special UITableViewCell that contains just a button?

then make that button cell the bottom row of the last group all the time. make the group so that it will be unlabeled and appear as if the button is sitting at the bottom of your tableview. this way you won't have to muck around with recalculating the tableview's frame and redrawing it.

if the tableview will scroll due to there being many rows, then you'd be calculating the height of the tableview up to a set max (at which point the tableview will need to scroll to show more rows).

once you've determined the height of the tableview you'll need to display your rows, make a frame of the appropriate size, set the tableview's frame to it, position the button just under the tableview, and then redraw the view.

the layout and positioning in this case will need to be done programmatically.

UITableview is a subclass of UIView, so you can change its frame to suit your needs just like a UIView, and UITableView will manage drawing itself to whatever frame you give it.

pxl
A: 

Just use the methods UITableViewDataSource and UITableViewDelegate provides you.

height = [self tableView:numberOfRowsInSection]*[self tableView:heightForRowAtIndexPath:] + A_CONSTANT_FOR_HEADER_AND_FOOTER_HEIGHT
sliver
A: 

I agree with pxl that adding a cell with the button in it may be the easiest way to accomplish what you want.

Whether or not you do that, the table view's sizeToFit method should resize the view to (just) fit its contents. If that doesn't work, you can use numberOfSections and rectForSection: to build a loop that determines the height of the table's contents, and reset its frame manually. (And then position the button underneath.)

Sixten Otto