简体   繁体   English

在采用动态大小的单元格并将内容偏移设置为顶部后,UITableView 没有一直滚动到顶部

[英]UITableView not scrolling all the way to top after resorting dynamic size cells and setting content offset to top

I have a tableview with dynamically sized cells, and a button that toggles the sort order of these cells.我有一个带有动态大小的单元格的表格视图,以及一个切换这些单元格的排序顺序的按钮。 I'd like to scroll to the top every time the sort order is toggled, but when I set the content offset to the top, it seems to only scroll ~90% of the way there.每次切换排序顺序时,我都想滚动到顶部,但是当我将内容偏移设置到顶部时,它似乎只滚动了大约 90% 的路径。

The offsetting code is simple enough and has served me well on different projects, so I seriously doubt the problem is here:抵消代码很简单,并且在不同的项目中为我提供了很好的服务,所以我严重怀疑问题出在这里:

- (void) scrollToTop
 {
    CGPoint offset = CGPointMake(0, -self.tableView.contentInset.top);
    [self.tableView setContentOffset:offset animated:YES];
}
    [self.tableView reloadData]; // Lets update with whatever info we have
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

reloading and then scrolling resolved for me.重新加载然后滚动为我解决了。

Didn't think I would find the answer so soon.没想到这么快就找到答案了。

I was using UITableView's tableView:estimatedHeightForRowAtIndexPath: to return my minimum cell height, and it seems that the tableview uses this inside reloadData to create an idea of how big the content is before actually dequeuing the cells and caching their height.我正在使用 UITableView 的tableView:estimatedHeightForRowAtIndexPath:返回我的最小单元格高度,并且似乎 tableview 在reloadData中使用它来创建在实际使单元格出列并缓存它们的高度之前内容有多大的想法。 Being halfway down the content and reloading the data causes the tableview to think the distance to the top is the (number of cells offscreen above the current visible * the minimum height from estimatedHeightForRow ), causing the tableview to only offset itself as if all cells were the minimum height.在内容的中途并重新加载数据会导致表格视图认为到顶部的距离是(当前可见屏幕上方的单元格数 * 来自estimatedHeightForRow的HeightForRow 的最小高度),导致表格视图仅偏移自身,就好像所有单元格都是最小高度。 My solution was just to avoid using the estimated height, since my tableview isn't excessively long anyway.我的解决方案只是避免使用估计的高度,因为我的 tableview 无论如何都不会太长。 If you do have a large tableview (approaching 1000+ rows) that actually needs to use the estimated values for performance reasons, you might want to find a way to make the estimated values as close to the runtime values as possible, or look into more detailed solutions.如果您确实有一个大表视图(接近 1000 多行),出于性能原因实际上需要使用估计值,您可能希望找到一种方法使估计值尽可能接近运行时值,或者查看更多详细的解决方案。

tl;dr - Remove tableView:estimatedHeightForRowAtIndexPath: and just allow the tableView to size itself from heightForRowAtIndexPath tl;dr - 删除tableView:estimatedHeightForRowAtIndexPath:并只允许 tableView 从heightForRowAtIndexPath调整自身大小

What about something like this instead?像这样的东西呢?

NSIndexPath *start = = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:start atScrollPosition:UITableViewScrollPositionTop animated:YES];

None of these worked for me.这些都不适合我。 The solution was to call layoutIfNeeded() before setting the content offset:解决方案是在设置内容偏移量之前调用layoutIfNeeded()

tableView.reloadData();
tableView.layoutIfNeeded();
tableView.setContentOffset(CGPoint.Empty, animated: true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM