为什么自调整单元格需要 estimatedRowHeight?

Why is estimatedRowHeight needed for self-sizing cell?

在设置您的 UITableView 用于自动调整单元格大小时,您必须指定 estimatedRowHeight。为什么甚至需要这个 - 考虑到单元格高度将在单元格显示之前首先由系统计算?

将行高设置为UITableViewAutomaticDimension (-1),告诉自动布局引擎它需要求解行高。估计高度为引擎提供了求解布局约束方程的起始猜测。

来自 estimatedRowHeight 的文档:

Providing a nonnegative estimate of the height of rows can improve the performance of loading the table view. If the table contains variable height rows, it might be expensive to calculate all their heights when the table loads. Using estimation allows you to defer some of the cost of geometry calculation from load time to scrolling time.

When you create a self-sizing table view cell, you need to set this property and use constraints to define the cell’s size.