I have a viewcontroller with a tableview that's with rowHeight property set to UITableViewAutomaticDimension:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var items: [String] = []
@IBOutlet weak var tableView: UITableView!
// MARK: - UITableViewDataSource, UITableViewDelegate
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.expandButton.setTitle(self.items[indexPath.row], for: .normal)
return cell
}
// MARK: - View cycle
override func viewDidLoad() {
super.viewDidLoad()
items = [
"Allen","Upton","Hu","Yuli","Tiger","Flynn","Lev","Kyle","Sylvester","Mohammad",
"Harlan","Julian","Sebastian","Porter","Preston","Palmer","Jakeem","Micah","Stephen","Tucker"
]
self.tableView.estimatedRowHeight = 150
self.tableView.rowHeight = UITableViewAutomaticDimension
}
}
The interface is defined in IB as follows. It has a custom cell. The cell contains a stackview. In the top part of that stackview, there's a button and in the bottom part, there's a textfield. The textfield is set to hidden initially.
Tapping the button will unhide the textfield. The cell however, does not expand:
Expected is that the cell expands to show the whole stackview. Why doesn't this work?
(I've gotten it to work by reloading the cell but this is an ugly workaround that requires code in the viewcontroller.)
Project on Github: https://github.com/bvankuik/TestResizingCell