0

I'm making a tableView programmatically like this

tableView = UITableView.init(frame: CGRectZero, style: .Plain)
tableView.delegate = self
tableView.dataSource = self
self.view = tableView

Then I'm setting its rowHeight value to

tableView.estimatedRowHeight = 250
tableView.rowHeight = UITableViewAutomaticDimension

Each cell is described in separate .swift-file and each of them has function 'construct()' which builds its own content (labels, buttons, etc.), here is the piece of their code

let btnControl = UIButton()
btnControl.frame = CGRectMake(avatar.frame.origin.x, avatar.frame.origin.y + avatar.frame.size.height + 10, screenWidth - 20, 30)
self.addSubview(btnControl)

At the end it looks like

So what's the problem?

Hreno Hrenovich
  • 336
  • 6
  • 16
  • 1
    maybe your constraint is wrong set, if u want to use `UITableViewAutomaticDimension `, u have to pin the largest view of your cell to top and bottom, then the cell only scale based on that – Tj3n Jan 14 '16 at 11:26
  • Maybe. I did not make any constraints at all. I've an image, 3 labels and button as you can see on the picture. Can you please help me with connecting image with row top and button with row bottom? With creating constraints programmatically, I mean. We're working in cell-swift-file, in function construct as I described in startPost – Hreno Hrenovich Jan 14 '16 at 11:32
  • In fact i've never create constraint with code so i can't really help u with that, why cant u use storyboard for it? just create 2 constraint to the top and bottom container, much harder if u try to create everything programmatically, faster way for programmatically is count the image or any view height then set your cell's height based on that in `heightForRow` – Tj3n Jan 14 '16 at 11:36

1 Answers1

0

If you need to create constraints programmaticaly, you will see everything you need in the documentation page : https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/index.html

The only other way I know to do what you are looking for is to get the size of each cell inside your "tableView(_:heightForRowAtIndexPath:)" implementation for UITableViewDelegate Protocol.

MessuKilkain
  • 76
  • 1
  • 1
  • 11