I would like to get the size of a UITableView\'s content view when the table is populated. Any suggestions on how to do this?
For table views whose heights are dynamically sized by their cells' contents--
My tableView
is contained in a UIView
, whose updateConstraints()
function looks like this:
override func updateConstraints() {
self.tableView.layoutIfNeeded()
self.tableViewHeight.constant = min(300, self.tableView.contentSize.height)
super.updateConstraints()
}
tableViewHeight
is an IBOutlet
to the XIB-assigned height of the table view. I get whichever is smaller--300 points, or the height of the table view's content size.
// Allows you to perform layout before the drawing cycle happens.
//-layoutIfNeeded forces layout early. So it will correctly return the size.
// Like dreaming before doing.
[tableView layoutIfNeeded];
CGSize tableViewSize=tableView.contentSize;
Here's a utility method that does it the hard way. The negligible advantage is there's no need to call [tableView layoutIfNeeded]
.
#define CGSizesMaxWidth(sz1, sz2) MAX((sz1).width, (sz2).width)
#define CGSizesAddHeights(sz1, sz2) (sz1).height + (sz2).height
+ (CGSize)sizeForTableView:(UITableView *)tableView {
CGSize tableViewSize = CGSizeMake(0, 0);
NSInteger numberOfSections = [tableView numberOfSections];
for (NSInteger section = 0; section < numberOfSections; section++) {
// Factor in the size of the section header
CGRect rect = [tableView rectForHeaderInSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
// Factor in the size of the section
rect = [tableView rectForSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
// Factor in the size of the footer
rect = [tableView rectForFooterInSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
}
return tableViewSize;
}
I don't know how much people will like this answer because I am not sure that I like it. But I managed to get something working with this.
This will work for dynamic height cells. The callback will get called a few times as the tableview figures out its contentView
class ContentSizeNotifyingTableView: UITableView {
var contentSizeDidChange: ((CGSize) -> ())?
override var contentSize: CGSize {
didSet {
self.contentSizeDidChange?(self.contentSize)
}
}
}