自调整单元格使 UITableView 跳跃

Self Sizing Cells make UITableView jump

我有一个 UITableView,每个单元格都有一个图像和两个标签,如第一张图片所示

所以我正在尝试使用自动调整大小的单元格,一切都很好,除了两件事:

1) 第一个和第二个单元格没有正确显示内容,但在我向下滚动然后向它们 return 后,一切正常。我尝试在 viewDidAppear 中使用 [tableview reloadData],但没有帮助。首先看标签,它们不合适。您可以在此视频中观看 https://www.youtube.com/watch?v=I9DGBl1c5vg

查看第一个单元格上的标签。

2) 这是一个艰难的过程。我正在滚动 table,一切都很好,但是当我 select 该行时,问题发生了。它把我推到详细视图,但是当我按 "back" 和 return 到主视图时,table 视图跳转,所以我不会回到 selected行,如果我滚动 table 它也不会平滑滚动,但会跳跃。这是两个视频的链接,第一个显示滚动很流畅 https://www.youtube.com/watch?v=9wAICcZwfO4 if i don't go to detail view, and the second shows the jumping problem https://www.youtube.com/watch?v=fRcQ3Za1wDM .

绝对肯定与自动调整大小的单元格有关,因为如果我不使用它,就会出现 none 这个问题。

很遗憾,我认为您提出的两个问题都是 IOS 个错误。

问题(1)

blog 建议了一个简单的修复方法。

When the table view is first displayed, you may find some of the cells are not sized properly. But when you scroll the table view, the new cells are displayed with correct row height. To workaround this issue, you can force a reload after the view appears:

只需将以下内容添加到您的 viewDidAppear 方法中。我已经测试过了,效果很好。

[self.tableView reloadData];

问题(2)

第二个问题与以下问题重复:

IOS 8 UITableView self-sizing cells jump/shift visually when a new view controller is pushed

问题作者自己提出了一个解决方法,似乎还可以。但是,我还没有尝试过这个。

Okay, I solved it by caching my cell heights in sizeThatFits, and returning that value for estimated cell heights within the delegate. Works beautifully.

请随时转到该问题以获取其他建议的解决方案。

好的,这两个问题我都解决了

问题 1

我添加了这两行

[cell.contentView setNeedsLayout];
[cell.contentView layoutIfNeeded];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathreturn cell

之前

问题二

解决方案看起来非常简单。我只需要实现 viewWillDissapear 并在其中重新加载数据,所以代码看起来像这样

- (void)viewWillDisappear:(BOOL)animated;{
    [super viewWillDisappear:animated];
    [self.tableView reloadData];
}

我的解决方案很简单,如果它对某人不起作用,我在这里找到了一些有用的链接。
1) UITableView layout messing up on push segue and return. (iOS 8, Xcode beta 5, Swift)
2) http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html