【问题标题】:Resizing UITableView to fit content on ios 11调整 UITableView 的大小以适应 ios 11 上的内容
【发布时间】:2018-06-08 02:15:48
【问题描述】:

鉴于 ios 11 上 UITableView 中的垃圾 contentSize 如何获得具有可变高度单元格的表格视图以自动调整为该内容?

这里的答案:Resizing UITableView to fit content 已经腐烂,不再适用于 ios 11

【问题讨论】:

  • 你找到解决办法了吗?
  • 不,我有一个混乱的蛛网,它不能可靠地计算 uitableview 的内在内容大小,estimatedRowHeight 会影响外观,而不仅仅是性能

标签: swift uitableview ios11 uitableviewautomaticdimension


【解决方案1】:

看起来当UITableView 使用自动尺寸 - UITableViewAutomaticDimension 时,它会在您滚动时动态计算contentSize

所以解决方案是使用 KVO 捕捉这些变化:

var contentSizeObservation: NSKeyValueObservation?

override func viewDidLoad() {
    super.viewDidLoad()

    contentSizeObservation = tableView.observe(\.contentSize, options: [.new], changeHandler: { tableView, value in
        // Do the setup here, this will be called multiple times
    })
}

重要

您需要将contentSizeObservation 定义为全局(作为类成员),否则changeHandler 块将不会被调用,因为该变量将被ARC 清除。

【讨论】:

    猜你喜欢
    • 2011-02-05
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2015-05-16
    • 2012-07-22
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多