-1

we are displaying some data in custom tableview cells but if some labels are empty we are hiding the labels and using uitableview.automaticdimension in estimated row height but not working well.Thanks in advance

image that how i got my tablecell

declared in

func viewDidLoad() {
    self.cabinetVC.estimatedRowHeight = 260
        self.cabinetVC.rowHeight = UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}
Rengers
  • 14,911
  • 1
  • 36
  • 54
kiran
  • 11
  • 2
  • 2
    Can you upload your code so I can tell you – Abhishek Jadhav Jan 30 '19 at 11:28
  • Did you set any constraints to the label? I answered another question here: https://stackoverflow.com/questions/52073107/uitableviewautomaticdimension-works-not-as-expected-swift/52073155#52073155 Maybe this helps too – Teetz Jan 30 '19 at 11:28
  • @ Abhishek Jadhav uploaded how i written my code is... – kiran Jan 30 '19 at 11:44
  • 1
    @kiran cross check your constraints also(top, bottom, Leading and trailing) and UILable number of lines should be zero – Yogita J Jan 30 '19 at 11:52

2 Answers2

0

You should comment or remove this delegate method,

/*func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

        return UITableView.automaticDimension
}*/

You are already providing some static value in viewDidLoad, that will helps to the proper sizing of the tableView cell.

Try and share the results.

EDIT: For 3x3 labels in a cell, you can do it as follows:

1) Take a vertical stackView

2) Add 3 Horizontal stackView to it.

3) Add 3 Labels to each horizontal stackView

4) Set numberOfLines to zero

5) Set equalWidth constraints to all inner stackViews and Labels.

6) Set leading, top, trailing and bottom to all stackViews and Labels.

Here is how it will look.

For the label, you don't have the text - set nil to it and it will be handled properly.

Bhavin Kansagara
  • 2,866
  • 1
  • 16
  • 20
0

Automatic dimension relies on Auto Layout to determine the height of the cell. That means that there must be non ambiguous constraints. Make sure you have constraints from the top of the cell to the bottom of the cell.

For example:

---------------
       |
   ---------
   | label |
   ---------
       |
---------------
Rengers
  • 14,911
  • 1
  • 36
  • 54