-1

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(ProfilePicture)
    cell.addSubview(username)
    cell.addSubview(postText)
    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?

  • Possible duplicate of [Swift UITableViewAutomaticDimension is not working](http://stackoverflow.com/questions/42970655/swift-uitableviewautomaticdimension-is-not-working) – Hapeki Mar 23 '17 at 18:18
  • Using storyboard will make this much easier as you will need to use constraints to setup your cell. The trick is to make sure the objects in your cell constrain all sides of the cell (top, leading, trailing and bottom). If you don't, autolayout can't calculate the size of the cell. – rmp Mar 23 '17 at 18:21
  • @rmp i prefer writing code and all my project is already written – Bruno Snickers Mar 23 '17 at 18:26
  • That's fine, you just need to write constraints programmatically for all your cell objects. – rmp Mar 23 '17 at 18:28
  • @rmp height constraints ? – Bruno Snickers Mar 23 '17 at 18:36
  • You need to constrain all sides, Top Leading, Trailing, Botton – rmp Mar 23 '17 at 18:37
  • @rmp but what values should i set? My tableview already has constraints – Bruno Snickers Mar 23 '17 at 18:38
  • Not your tableview, the cell's content view. The objects in the cell need to constrain against the cell's content view so the cell will know what size it needs to be. – rmp Mar 23 '17 at 18:42
  • @rmp Everytime i try to add constraints here is the error i get `"UILabel:0x7fa38dd01df0'Champagnepapi'.top"> and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'` – Bruno Snickers Mar 23 '17 at 18:45
  • And here are the constraints `username.anchor(cell.topAnchor, left:ProfilePicture.rightAnchor, bottom: nil, right: cell.rightAnchor, topConstant: 40, leftConstant: 15, bottomConstant: 0, rightConstant: 15, widthConstant: 0,heightConstant:30)` – Bruno Snickers Mar 23 '17 at 18:46
  • This might help you: http://stackoverflow.com/questions/40299190/swift-3-create-uilabel-programmatically-and-add-nslayoutconstraints – rmp Mar 23 '17 at 18:49
  • I know how to use constraints,but they don't work in cells – Bruno Snickers Mar 23 '17 at 18:50

1 Answers1

1

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

enter image description here

rmp
  • 3,503
  • 1
  • 17
  • 26
  • Thanks homey.Yer answer helped me and hopefully will help future generations when they will be working on their app and work for their dreams. – Bruno Snickers Mar 23 '17 at 18:54