Table View Cell Row Height doesnt work
Asked Answered
F

8

30

enter image description here

I have tried to hard code the row height in the table view cell. After running the program it looks like only one line. I suspect it is because of the height of the row of the table view.

Could you please tell me what went wrong in here?

Floribunda answered 2/10, 2017 at 4:35 Comment(3)
no issue seems with height, have you also set height from delegate method in code? If so remove that method.Muchness
what is your label line number? it should be 0 and what about your constraint set ? would you share with that on the other hand did you set for dynamic constraint table view height?Surrealism
You can check my answer hope this will help you https://mcmap.net/q/379807/-swift-dynamic-table-cell-height-duplicateGaven
F
53

In Xcode 9, tableview automatically create estimated cells based on Autolayout. Since you have not provide autolayout, that's why you are getting this.

You need to disable estimated and automatic cell size by selecting tableview and uncheck the two options:

enter image description here

Give value you want to Row height, and cell will be created accordingly.

Hope this can help.

Furnish answered 2/10, 2017 at 5:10 Comment(1)
What can I do if I have different kind of prototype cells with different heights?Welterweight
M
9

Add this code in your UITableViewDelegate

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}
Matland answered 2/10, 2017 at 4:43 Comment(3)
No, do not do this. If all rows have the same height it is better to simply set the table view's rowHeight property once in viewDidLoad. Only implement this delegate method if different rows have different heights.Nor
@Nor As per apple doc, delegate have better performance then property.Nabokov
@Nabokov You misread the documentation. The documentation for UITableView rowHeight and UITableViewDelegate heightForRowAt clearly state that the delegate method is a performance issue. It is far more efficient to set the rowHeight property than use the delegate method assuming all rows have the same height.Nor
U
4

Ensure following steps to make, auto dimension effective for cell/row height layout. (Here is sample code for UILabel and how to set content specific height for table cell.)

  • Assign and implement dataSource and delegate
  • Assign UITableViewAutomaticDimension to rowHeight & estimatedRowHeight
  • Implement delegate/dataSource methods (i.e. heightForRowAt and return a value UITableViewAutomaticDimension to it)

-

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    // Swift 4.2 onwards
    table.rowHeight = UITableView.automaticDimension
    table.estimatedRowHeight = UITableView.automaticDimension


    // Swift 4.1 and below
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension

}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // Swift 4.2 onwards
    return UITableView.automaticDimension

    // Swift 4.1 and below
    return UITableViewAutomaticDimension
}

For label instance in UITableviewCell

  • Set number of lines = 0 (& line break mode = truncate tail)
  • Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
  • Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.

enter image description here

Ulla answered 23/10, 2017 at 9:56 Comment(0)
E
3

Set row height as well as estimated row height as Automatic in viewDidLoad() as

table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension

In heightForRowAt: set height as UITableViewAutomaticDimension as

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Note: Set number of lines of label long restaurant name as 0.

Emlen answered 23/10, 2017 at 10:9 Comment(0)
R
3

You can manually do it in your tableviewdelegate:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}
Recombination answered 17/6, 2019 at 20:21 Comment(0)
R
1
  1. Set number of lines to 0
  2. Set
   yourTableView.rowHeight = UITableView.automaticDimension and 
   yourTableView.estimatedRowHeight =  UITableView.automaticDimension
  1. return UITableView.automaticDimension in tableView(_:heightForRowAt:) delegate method

  2. Set all constraints . Bottom constraint is important to increase table view cell height .

Your all set to go

Roadhouse answered 22/1, 2020 at 10:10 Comment(0)
K
0

If you want the cells to occupy space according to its content, you need to return UITableViewAutomaticDimension in the table view delegate methods heightForRowAt: and estimatedHeightForRowAt:

Kayser answered 2/10, 2017 at 14:31 Comment(1)
you can also use: yourTable.rowHeight = UITableViewAutomaticDimension yourTable.estimatedRowHeight = UITableViewAutomaticDimensionKayser
W
0

Following this guide which worked for me, you have to add a bunch of constraints and then the rowheight is calculated automatically.

In short, what I did: (adding and modifying constraints are the two rightmost buttons bottom right of the storyboard view)I at first selected all views in the prototype cell, deleted all constraints (there is a nice option on the rightmost button), then added all missing constraints (another nice option there) and last I chose the second button from the right and clicked all the red lines. You can do this while having all views selected at the same time. And then it worked.

Welterweight answered 24/1, 2019 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.