Reputation: 11
My UITableView is embedded within a UIView
, I would like that this superview's height change according to the number of UITableViewCell
s the table view has.
That mean, if the tableView has 1 cell, UIView
will be the size of this cell, if there are 2 cells, the UIView
will grow to the size of 2 cells.
Upvotes: 0
Views: 566
Reputation: 12208
If you are using Autolayout constraints, you can just update UITableView's height constraint itself and that should do the job.
Here are the basic steps:
Subclass UITableView:
class CustomTableView: UITableView {
var maximumTableViewHeight: CGFloat = UIScreen.main.bounds.size.height
private var heightConstraint: NSLayoutConstraint!
override func reloadData() {
super.reloadData()
self.heightConstraint.constant = min(self.contentSize.height, maximumTableViewHeight)
}
override func awakeFromNib() {
super.awakeFromNib()
self.heightConstraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil,
attribute: .notAnAttribute, multiplier: 1, constant: 0)
self.addConstraint(heightConstraint)
}
}
Storyboard that has our subclassed CustomTableView
Then your view controller:
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet var tableView: CustomTableView!
private let cellIdentifier = "Cell"
var dataSource: [Int] = [1, 2, 3] {
didSet {
self.tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.maximumTableViewHeight = 400 // Adjust as per your max height
}
@IBAction func addCell(_ sender: Any) {
let count = self.dataSource.count + 1
self.dataSource.append(count)
let scrollToIndexPath = IndexPath(row: count - 1, section: 0)
DispatchQueue.main.async {
self.tableView.scrollToRow(at: scrollToIndexPath, at: .bottom, animated: false)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
}
cell.textLabel?.text = "Value: " + String(dataSource[indexPath.row])
return cell
}
}
That yields:
Notice the red area, thats the parent UIView
of UITableView
Upvotes: 0
Reputation: 11200
I would add one more UIView
to UITableViewCell
's contentView
which I would use as content view. Then I would set backgroundColor
of real contentView
Upvotes: 1