[英]Auto-layout issue on scroll
I have .xib
which has some auto-layout like so (might need to click to zoom): 我有
.xib
,它具有这样的自动布局(可能需要单击以放大):
Nothing special, I have 13pt
spacing with the border of the superview. 没什么特别的,我与
13pt
的边框之间有13pt
间距。 This xib is then loaded in the code into a tableViewCell. 然后将此xib在代码中加载到tableViewCell中。
I'm also setting the height in the viewDidLoad
of the table like so: 我还在表的
viewDidLoad
中设置高度,如下所示:
[self.tableView setEstimatedRowHeight:40];
[self.tableView setRowHeight:UITableViewAutomaticDimension];
My cellForRowAtIndexPath is: 我的cellForRowAtIndexPath是:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId forIndexPath:indexPath];
---OR--- (Both don't make a difference) ---或---(两者都不重要)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
Now the fun begins. 现在,乐趣开始了。 The auto-layout works when it wants to, and sometimes I scroll a cell out of view, bring it back and it's not the same size.
自动布局可以在需要时起作用,有时我会滚动一个单元格使其不可见,将其放回原处,并且它的大小不一样。 See image below:
见下图:
Any ideas on what I am doing wrong? 关于我在做什么错的任何想法吗?
PS: Not in the screengrab here, but I do have some cells with a lot of text that look like the same height of the big cell on left. PS:这里不在屏幕抓屏中,但是我确实有一些单元格,其中包含很多文本,看起来与左侧大单元格的高度相同。
PPS: I've tried the following with no success (based on this article http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html ): PPS:我尝试了以下操作均未成功(基于本文http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html ):
[self.tableView reloadData];
[self.tableView reloadData];
in the viewDidLoad I had the same issue, it seems if you set the text of the label exclusively in - tableView:willDisplayCell:forRowAtIndexPath:
instead of - tableView:cellForRowAtIndexPath:
then you will observe the behaviour of random incorrectly sized cells. 我遇到了同样的问题,似乎如果仅在
- tableView:willDisplayCell:forRowAtIndexPath:
而不是- tableView:willDisplayCell:forRowAtIndexPath:
- tableView:cellForRowAtIndexPath:
设置标签的文本,那么您将观察随机大小不正确的单元格的行为。
I've created a test project to demonstrate this https://github.com/sja26/iOS-TestSelfSizingCellsWithUILabel 我创建了一个测试项目来演示此https://github.com/sja26/iOS-TestSelfSizingCellsWithUILabel
One reason you get this behaviour - you use 出现这种行为的原因之一-您使用
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Try to use 尝试使用
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
instead. 代替。 This will reuse your cell with the same size as needed
这将根据需要重新使用您的单元格
And, of course check heightForRowAtIndexPath
method for properly setup value 并且,当然要检查
heightForRowAtIndexPath
方法以获取正确的设置值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.