Swift 3 - How to resize Uitableview automatically

Asked

Viewed 208 times

1

How to automatically resize a Uitableview according to the amount of content in it?
I want to do something like what you can do with an Uitextview:

MyTextView.sizeToFit()
  • Your question does not make much sense because the Uitableview is made to accommodate infinite elements and show them with scroll. Or you’re talking about the elements inside contentView inside tableCell?

  • No @leofontes, this is the same tableView. Somehow, is there any way to calculate the total height of the lines inside this tableView? What I want to do is take this size, set {tableView.frame.size.height} and put this all in a scrollview

2 answers

2


You need to take the height of the cell, ask for the array Voce uses as datasource count and then set the size in the frame, follows 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 don’t think your question makes much sense, because Tableview was designed to accommodate the items inside without increasing their height, but good luck!

2

Simple, just enter this code into your viewDidLoad:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 76

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

Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.