滚动

时间:2015-08-04 14:31:38

标签: ios xcode autolayout interface-builder

我有.xib有一些像这样的自动布局(可能需要点击放大): enter image description here

没什么特别的,我与superview的边界有13pt间距。 然后将此xib加载到tableViewCell中。

我也在表的viewDidLoad中设置高度,如下所示:

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

我的cellForRowAtIndexPath是:

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

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];

现在开始变得有趣了。自动布局在需要时工作,有时我将一个单元格滚出视图,将其带回来并且它的大小不同。见下图: enter image description here

关于我做错的任何想法?

PS:不在屏幕上这里,但我确实有一些单元格有很多文字看起来像左边大单元格的高度。

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

  • 在viewDidLoad
  • 中添加了[self.tableView reloadData];
  • 我删除了标签
  • 上的显式和首选宽度

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,如果您在- tableView:willDisplayCell:forRowAtIndexPath:而不是- tableView:cellForRowAtIndexPath:中专门设置标签的文字,那么您将会观察到随机尺寸不正确的单元格的行为。

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

答案 1 :(得分:1)

您遇到此行为的一个原因 - 您使用

cv::

尝试使用

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

代替。这将重复使用与您需要的相同大小的单元格

当然,检查cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 方法以获得正确的设置值

相关问题