如何获得UITableView的内容视图的大小?

时间:2021-11-02 20:23:23

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?

我希望在填充表时获得UITableView的content视图的大小。有什么建议吗?

4 个解决方案

#1


52  

// 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;

#2


5  

Here's a utility method that does it the hard way. The negligible advantage is there's no need to call [tableView layoutIfNeeded].

这里有一个实用的方法,它很难做到。可以忽略的优点是不需要调用[tableView layoutifneed]。

#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;
}

#3


1  

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:

我的tableView包含在UIView中,它的updateconstr()函数看起来是这样的:

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.

tableViewHeight是表视图的xib指定高度的IBOutlet。我得到一个较小的值——300个点,或者表视图的内容大小的高度。

#4


1  

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

这将适用于动态高度单元格。当tableview显示它的contentView时,回调会被调用几次。

class ContentSizeNotifyingTableView: UITableView {
    var contentSizeDidChange: ((CGSize) -> ())?

    override var contentSize: CGSize {
        didSet {
          self.contentSizeDidChange?(self.contentSize)
        }
    }
}

#1


52  

// 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;

#2


5  

Here's a utility method that does it the hard way. The negligible advantage is there's no need to call [tableView layoutIfNeeded].

这里有一个实用的方法,它很难做到。可以忽略的优点是不需要调用[tableView layoutifneed]。

#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;
}

#3


1  

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:

我的tableView包含在UIView中,它的updateconstr()函数看起来是这样的:

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.

tableViewHeight是表视图的xib指定高度的IBOutlet。我得到一个较小的值——300个点,或者表视图的内容大小的高度。

#4


1  

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

这将适用于动态高度单元格。当tableview显示它的contentView时,回调会被调用几次。

class ContentSizeNotifyingTableView: UITableView {
    var contentSizeDidChange: ((CGSize) -> ())?

    override var contentSize: CGSize {
        didSet {
          self.contentSizeDidChange?(self.contentSize)
        }
    }
}