So I\'m trying to build something rather intricate - potentially more than what the picture below shows. Anyway..
I\'m trying to have a UITableView
inside o
You should use UIStackView
instead UITableView
in cells, because as i understood, inner tableViews will not scroll or dequeue. That will be misusage of UITableView
.
If you have already cells or you insist, you may set height constraint of inner tableview at layoutSubviews
method of inner tableview's parent view by taking tableviews content size.
You can do with Observer, please try if it works
1. You have to add to tableview (which is in tableCell)
yourTableView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)
2. Method that observe changes
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
//height can be use as cell height,
let height = yourtableview.contentSize.height;
}
deinit {
yourtableview.removeObserver(self, forKeyPath: "contentSize")
}
You can subclass UITableView
and turn it into an "auto-sizing" table view, based on its contents:
final class ContentSizedTableView: UITableView {
override var contentSize:CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
}
Now, the table view will behave very similar to a multi-line UILabel
--- just set up your constraints in the same way.
Two notes:
1) Disable scrolling on the ContentSizedTableView
- not necessary but will probably be a better UX for your case.
2) When you lay this out in your prototype cell, it will need a Height constraint to satisfy IB / Storyboard. So, add either
>=
height constraint (so you have a minimum height even if you have no rows), orHere is an example using a ContentSizedTableView
in a scroll view. Same idea, and should make it clear how to use it:
https://stackoverflow.com/a/56840758/6257435