Static UITableView, create a separate cell with dynamic height in Swift

I have a static UITableView that has 12 rows, and for 11 rows I know what the height should be.

I have a UILabel that has dynamic text that is inside the 12th row, how can I make only one cell to have a dynamic height based on the text in UILabel.

I tried the code below inside viewDidLoad , but it did not work. The lines remained the same height. I also set lines = 0 for UILabel

  tableView.estimatedRowHeight = 100.0 tableView.rowHeight = UITableViewAutomaticDimension 
+7
ios uitableview swift
source share
6 answers

Have you tried this?

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if (indexPath.row < 11) { // Everything starts at 0, so this covers 0-10 // Whatever your cell height is return <#fixedCellHeight#> } else { // This is your 12th cell (indexPath.row 11), as we start counting at 0 return UITableViewAutomaticDimension } } 
+9
source share

Try the following:

 func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } 
+1
source share

You can create cell cells of the cell prototype to represent the table. You give them 2 different identifiers. And then in your code you override this selection

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = UITableViewCell() if indexPath.row < 12 { cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) } else { cell = tableView.dequeueReusableCellWithIdentifier("cell12", forIndexPath: indexPath) } return cell } 
0
source share

You can create a dynamic cell with the height of the text, then you can set the static and dynamic cell as a table

Here is a link to a dynamic cell with text How to dynamically change cell height in a static cell UITableView

0
source share

Adding @Adrian to the answer if you are using static cells, changing one cell to dynamic height and others, as you can edit it.

  override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if indexPath.row == 11 { // This is your 12th cell (indexPath.row 11), as we start counting at 0 return UITableViewAutomaticDimension } else { return super.tableView(tableView, heightForRowAtIndexPath: indexPath) } } 
0
source share

More elegant could simply be implemented as suggested:

 func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } 

Or in Objective C:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } 

But instead of defining a specific row, add restrictions on the contents of the static cells in the Storyboard to keep the row height constant. Thus, if you move lines or change content, you do not need to change any code.

0
source share

All Articles