【问题标题】:How to get the size of a UITableView's content view?如何获取 UITableView 的内容视图的大小?
【发布时间】:2011-07-28 14:03:13
【问题描述】:

我想在填充表格时获取 UITableView 的内容视图的大小。有关如何执行此操作的任何建议?

【问题讨论】:

  • 为什么CGSize tableViewContentSize = tableView.contentSize 不起作用?
  • reason is [tableView layoutIfNeeded];// 允许您在绘制周期发生之前执行布局。 -layoutIfNeeded 尽早强制布局。因此它会正确返回大小。就像做梦之前做的那样。

标签: iphone uitableview


【解决方案1】:
// 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;

【讨论】:

  • + 1 就这么简单,因为表格视图会根据 rowHeight 预先计算高度,或者在显示之前为每一行调用代表 heightForRow。
  • 当表格根据其内容动态调整大小时,这似乎不起作用。
  • 对我来说很好!
  • @fatuhoku 成功了,你需要在 CellForRow 的 TableView 的最后一个单元格之后添加这一行
  • 我的情况略有不同,但关键是设置estimatedRowHeight = 0 _tableView.estimatedRowHeight = 0;
【解决方案2】:

这是一个实用的方法,它很难做到。微不足道的优势是无需致电[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;
}

【讨论】:

    【解决方案3】:

    对于高度根据单元格内容动态调整大小的表格视图--

    我的tableView 包含在UIView 中,其updateConstraints() 函数如下所示:

    override func updateConstraints() {
        self.tableView.layoutIfNeeded()
        self.tableViewHeight.constant = min(300, self.tableView.contentSize.height)
        super.updateConstraints()
    }
    

    tableViewHeight 是一个 IBOutlet 到 XIB 分配的 table view 高度。我得到较小的值——300 点,或者表格视图内容大小的高度。

    【讨论】:

      【解决方案4】:

      我不知道有多少人会喜欢这个答案,因为我不确定我是否喜欢它。但我设法得到了一些东西。

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

      class ContentSizeNotifyingTableView: UITableView {
          var contentSizeDidChange: ((CGSize) -> ())?
      
          override var contentSize: CGSize {
              didSet {
                self.contentSizeDidChange?(self.contentSize)
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 1970-01-01
        • 2014-09-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多