0

I am having the problem in setting the dynamic height for my table view cell.I have used the following code to set it, but it's not working in fact now the cell height has even increased.

See this the code I have added in my project:

 override func viewDidLoad() {
super.viewDidLoad()

        tableview.rowHeight = UITableViewAutomaticDimension
        print(tableview.rowHeight)
        tableview.estimatedRowHeight = 120

}

This the screenshot for the constraints of the table view cell contents: Constraints for my content view of the table view cell

Please have a look at the constraints as well. Thanks in advance :)

Samarth Kejriwal
  • 1,168
  • 2
  • 15
  • 30

2 Answers2

1
  • If u have done everything correct and still u r unable the get desired output means the issue will be with constraints.

  • I have made a simple constraint layout, use this skeleton as reference and re-constraint your cell. enter image description here

  • Add (W:20, H:20) to [img].

  • Make sure you have set number of lines to 0, and all other automatic dimension stuff's.

  • IB Constraint Screenshot.

enter image description here

OutPut:

enter image description here

Community
  • 1
  • 1
Gokul G
  • 2,046
  • 15
  • 22
  • you havent defined the top and leading constraints ..should i define them as well ? – Samarth Kejriwal Jun 26 '17 at 10:13
  • just keep it as reference.. the image may have missed some constraint.. try to follow the same – Gokul G Jun 26 '17 at 10:14
  • wait i am setting the constraints and then i will send u the screen shot .and then check – Samarth Kejriwal Jun 26 '17 at 10:24
  • the problem is that my indicesname label is covering the whole width when the indices name is too long and so my curr price item is getting hidden,should i set a desired height for it ? – Samarth Kejriwal Jun 26 '17 at 10:29
  • if width of indices name is your problm.. add a `Horizontal constraint` between `indices` and `price`. change it's relation to `>=` and constant to 10 – Gokul G Jun 26 '17 at 10:32
  • i had set the width of the indices name as proportional to 0.7 of the width of the content view – Samarth Kejriwal Jun 26 '17 at 10:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147607/discussion-between-gokul-and-samarth-kejriwal). – Gokul G Jun 26 '17 at 10:35
  • actually when i was setting the proportional height for my image view in the table view cell then my table view cell height was abruplty expanded, is there any other way to handle this . I am having this problem while setting a proptional height for image view , if i set a proportional width to my image view along with an aspect ration then i dont ge this problem – Samarth Kejriwal Jul 03 '17 at 04:51
  • hi, continue this discussion in chat – Gokul G Jul 03 '17 at 05:05
  • help needed regarding ios deployment version – Samarth Kejriwal Jul 04 '17 at 07:33
  • Hi Gokul, needed some help, message me when you are free – Samarth Kejriwal Aug 26 '17 at 06:10
0

You may consider using func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat. It's a method in the UITableViewDelegate and you can use it to return a specific height for the rows you want. https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614998-tableview

This link may help: Dynamic tableViewCell height

Mihir Thanekar
  • 508
  • 4
  • 8