bpiec bpiec - 1 year ago 67
iOS Question

How to set automatic height of static cell in UITableViewController?

I am using IB and storyboard and I defined

. There I defined three static cells. The first two should have a given height but the last one should automatically fill the remaining space (anchor to the bottom of the view). How can I achieve this? I can use autolayout for the controls inside the cell but it is greyed out for the cell itself.

The best solution would be to do this from IB but programmatic solutions are also welcomed.


This is how it looks in my storyboard (colors are for visualizing separate controls, each control has required constraints):
Storyboard view controller

And after launching:
Simulator view
Note that the text view ends below the bottom of the screen.

I forgot to mention that I target iOS 7.0+.

Answer Source

Ok try this

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{

    if indexPath.row == 0 { 
        return firstRowHeight
    else if indexPath.row == 1 { 
        return secondRowHeight 
    else { 
        return tableView.frame.size.height - firstRowHeight - secondRowHeight 

The only thing that may be a problem is that this method is going to run before the view sets the tableView height