Setting tableView width equal to the maximum table’s cell width?

147 Views Asked by At

I am wondering if there is a way to set the width of a UITableView equal to the maximum cell width inside the table?

I added a left constraint to my table and for the widthAnchor constraint I set it equal to tableView.contentSize.width. However, this did not give me the results I am looking for.

1

There are 1 best solutions below

0
ChillY On

You could have calculation of the cell width as a separate function (static function maybe), that takes in arguments required for displaying the cell (string, label height, max width, etc...). Having this input you can calculate what would be the width of the widest cell when the data is updated and thus setting a constraint on tableview width with the desired width from cell width calculation.