-1

Im trying to UITableAutomaticDimension for a UITableCell I have. But it doesn't seems to be working

I have tried with this 2 methods.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            tableView.estimatedRowHeight = 250
            tableView.rowHeight = UITableViewAutomaticDimension
        return UITableViewAutomaticDimension

    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

I have also read a lot of stack overflow links that talk about that. Like this one or this one. But none of them worked.

I tried to add this code to my table view to make it work. But it doesn't

NSLayoutConstraint.activate([
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        tableView.topAnchor.constraint(equalTo: view.topAnchor),
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            ])

Of course Im reloading the cell and giving 0 lines to the label I have inside.

cell.sizeToFit()
cell.labelTitle.numberOfLines = 0

I also tried to add these 2 lines in the viewDidLoad() method

tableView.estimatedRowHeight = 250
tableView.rowHeight = UITableViewAutomaticDimension

enter image description here

What am I doing wrong?

Jalil
  • 1,167
  • 11
  • 34
  • First, you don't need to implement either `heightForRowAt` nor `estimatedHeightForRowAt`. Are you designing your cell as a prototype in Storyboard? Or purely via code? If code only, show your full cell subclass. If prototyping in Storyboard, show a screen-cap of your cell including all the constraints you've added. – DonMag Aug 26 '19 at 19:55
  • I have my cell in the storyboard but I deleted all the constraints to add them in the code. Like showed above – Jalil Aug 26 '19 at 20:03
  • Why are you trying to set constraints in code, instead of in your Storyboard prototype cell? Plus, none of the code you included in your question was even remotely related to the cell constraints. – DonMag Aug 26 '19 at 20:05
  • Because xcode doesn't let me to add constraints to the UITableViewCell from the storyboard. They are all in grey – Jalil Aug 26 '19 at 20:07
  • OK, your confusion is that you're trying to add constraints to the **Cell** when you really want to add constraints to the cell's **content**. So, if you add a label to the cell, you need to constrain the label to the cell (or the cell's contentView). – DonMag Aug 26 '19 at 20:09
  • I just uploaded the photo @DonMag – Jalil Aug 26 '19 at 20:19

1 Answers1

2

Here is a simple, one-label cell prototype:

enter image description here

I gave the label a yellow background, so it's easy to see its frame.

If you do this, and set the label's number of rows to Zero, the cells will auto-size themselves without any code.

Result:

enter image description here

Here is all the code needed to produce that output:

class SimpleLabelCell: UITableViewCell {
    @IBOutlet var theLabel: UILabel!
}

class SimpleTestTableViewController: UITableViewController {

    let theData: [String] = [
        "A short message.",
        "A medium length message, longer than short.",
        "A long message. This one should be long enough to wrap onto multiple lines, showing that this message bubble cell will auto-size itself to the message content.",
        "Another short message.",
        "Another medium length message, longer than short.",
        "Another long message. This one should be long enough to wrap onto multiple lines, showing that this message bubble cell will auto-size itself to the message content.",
    ]

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return theData.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SimpleCell", for: indexPath) as! SimpleLabelCell
        cell.theLabel.text = theData[indexPath.row]
        return cell
    }

}
DonMag
  • 69,424
  • 5
  • 50
  • 86