Bruno Snickers Bruno Snickers -4 years ago 200
Swift Question

Swift UITableViewAutomaticDimension is not working

I have got this table and cell

tableView = UITableView()
tableView.dataSource = self
tableView.delegate = self
tableView.allowsSelection = false;
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension

var postTexts = ["Nirvana"]
var posters = ["Champagnepapi"]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postTexts.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let ProfilePicture = UIImageView(frame: CGRect(x:19, y: 14, width: 50, height: 50))
ProfilePicture.image = #imageLiteral(resourceName: "cole")
ProfilePicture.layer.cornerRadius = 25
ProfilePicture.layer.masksToBounds = true

let username = UILabel(frame: CGRect(x: 80, y: 14, width: 300, height: 30))
username.text = posters[indexPath.row]

let postText = UITextView(frame: CGRect(x:75,y:40,width:200,height:200))
postText.text = "I rather be dead than cool - Kurt Cobain"

let border = UIView(frame: CGRect(x:0,y:150,width:350,height:1))
border.backgroundColor = UIColor(r: 217, g: 221, b: 219)

cell.addSubview(border )

return cell

I wanted my cell to have automatic height but this it the result i get
enter image description here

How can i get an automatic height for my cells?

Answer Source

You needs to set the constraints on your cell objects like this so the cell can calculate the size needed.

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download