0

I'm trying to set dynamic row height in TableView. But unfortunately is not working.

The only thing is working is to set the heightForRowAt indexPath to a constant number.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 470 //or any other number
}

If I delete this function, or try to set it to automatic the cell will not show at all. Something like this:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

I also tried to use:

self.tableView.estimatedRowHeight = UITableView.automaticDimension //Tried even with a number
self.tableView.rowHeight = UITableView.automaticDimension

But seems nothing to work besides using a constant height in heightForRowAt indexPath function. Which is not what I want, I want cell to be expanded based on content inside the cell.

I also tried to set height in heightForRowAt indexPath function to automatic but set a fixed height on constraints in the content of the cell xib file, still same (I did just to see what happens). So seems, something is wrong around this function.

Note: My TableView and UITableViewCell are divided in a storyboard and a xib files.

ee.bt
  • 308
  • 1
  • 4
  • 9
  • Show how you have your "cell content" constrained. – DonMag Nov 10 '21 at 19:06
  • Does this answer your question? [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – Ely Nov 10 '21 at 19:19
  • You need to provide enough internal constraints to determine the size of the cell without an explicit value. – Alain Bianchini Nov 11 '21 at 00:19

1 Answers1

2

If you want to set the dynamic tableview row height, you must use autolayout.

Please set the constraint on the UI components in the cell as shown in the image below.

enter image description here

I used UILabel as sample code. For UILabel, be sure to set Lines to 0.

Here is the sample code

class ViewController: UIViewController {
    
    let stringArr = ["aaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"]
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.dataSource = self
        
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableView.automaticDimension
    }
}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return stringArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as? TestCell else {
            return UITableViewCell()
        }
        
        cell.label.text = stringArr[indexPath.row]
        
        return cell
    }
}
  • You need to register the cell in the tableview.

tableView.register(UINib(nibName: “YourCellNibName”, bundle: nil), forCellReuseIdentifier: “YourCellReuseIdentifier”)

SEUNGHWAN LEE
  • 204
  • 1
  • 4
  • I did like this, and the cell is not showing at all. – ee.bt Nov 11 '21 at 07:34
  • I updated my question, adding a note. Note: My TableView and UITableViewCell are divided in a storyboard and a xib files. – ee.bt Nov 11 '21 at 08:21
  • Did you register the cell in the tableview? If you have not registered, please register as follows: //In viewDidLoad// tableView.register(UINib(nibName: “YourCellNibName”, bundle: nil), forCellReuseIdentifier: “YourCellReuseIdentifier”) – SEUNGHWAN LEE Nov 11 '21 at 08:59
  • Yep I did, and cell is showing when I set a fixed height in `heightForRowAt indexPath` function. No matter what, I do, when set to automatic or I delete the function, in those cases cell is not showing. I also tried to set it to automatic but set a fixed height on constraints in the content of the cell xib file. Still same. – ee.bt Nov 11 '21 at 09:02
  • Ok, found the solution. In `cellForRowAt indexPath` function the previous dev in return used not the `cell` but `UITableViewCell`. I didn't notice that, after seeing that I fixed. I'll mark this answer as correct, cause you already wrote it in your answer. – ee.bt Nov 11 '21 at 09:19