theseanything
theseanything

Reputation: 21

Create a view table with static cells of equal height to fill screen

How do I create a table view with static cells, where the row heights are all equal to make the set of static cells fill the table view?

The row height needs to be adjusted dynamically to account for the different iOS screen sizes.

For example I want a table view with 7 static cells of equal heights that fill the rest of the screen space except the navigation bar.

I've tired using the following with no luck:

self.tableView.rowHeight = self.tableView.frame.height/6;

and

override func tableView(tableView: UITableView, 
    heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{
    return self.tableView.frame.height/6;
}

and the autoLayout feature in storyboards is grayed out when I try to use it with any part of the table view.

Appreciate any help thanks!

Upvotes: 0

Views: 906

Answers (1)

Katz
Katz

Reputation: 866

  • Define auto layout constraints for your prototype cell.
  • Specify the estimatedRowHeight of your table view.
  • Set the rowHeight of your table view to UITableViewAutomaticDimension.
  • If we express the last two points in code, it looks like this.

    tableView.estimatedRowHeight = 44.0

    tableView.rowHeight = UITableViewAutomaticDimension

Upvotes: 0

Related Questions