Swift 3 - How to automatically resize UITableView

1

How to automatically resize a UITableView according to the amount of content in it?
I want to do something like what I can do with a UiTextView:

MyTextView.sizeToFit()
    
asked by anonymous 17.02.2017 / 15:52

2 answers

2

You need to get the height of the cell, ask for the array that you use as the dataSource of the count and then set the size in the frame, here's an idea:

var tamanhoCell = tableView.rowHeight
var count = seuArray.count
tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tamanhoCell * count);

I still think your question does not make much sense, because TableView was meant to accommodate the items in there without increasing its height, but good luck!

    
17.02.2017 / 16:22
2

Simple, just insert this code into your viewDidLoad:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 76

In the first line, it will scale automatically. In the second, you estimate the most used height. In theory, you only need the first, but I recommend using both.

Hugs.

    
26.03.2017 / 04:51