滚动时的自动布局问题

Auto-layout issue on scroll

我有 .xib,它有一些像这样的自动布局(可能需要点击缩放):

没什么特别的,我与超级视图的边框有 13pt 间距。 这个 xib 然后在代码中加载到 tableViewCell 中。

我还在 table 的 viewDidLoad 中设置高度,如下所示:

[self.tableView setEstimatedRowHeight:40];
[self.tableView setRowHeight:UITableViewAutomaticDimension];

我的 cellForRowAtIndexPath 是:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId forIndexPath:indexPath];

---或---(两者没有区别)

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];

现在好戏开始了。自动布局在需要时工作,有时我将一个单元格滚动到视图之外,将其放回原处,但大小不一样。见下图:

对我做错了什么有什么想法吗?

PS:不在此处的屏幕截图中,但我确实有一些包含大量文本的单元格看起来与左侧大单元格的高度相同。

PPS:我试过以下但没有成功(基于这篇文章http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html):

你出现这种行为的一个原因 - 你使用

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

尝试使用

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

相反。这将根据需要重复使用具有相同大小的单元格

而且,当然要检查 heightForRowAtIndexPath 正确设置值的方法

我遇到了同样的问题,似乎如果您只在 - tableView:willDisplayCell:forRowAtIndexPath: 而不是 - tableView:cellForRowAtIndexPath: 中设置标签文本,那么您会观察到随机大小不正确的单元格的行为。

我创建了一个测试项目来演示这个https://github.com/sja26/iOS-TestSelfSizingCellsWithUILabel