当重新加载具有不断变化的单元格高度的单元格时,UITableView会滚动到顶部 [英] UITableView scrolls to top when reloading cells with changing cell heights

查看:125
本文介绍了当重新加载具有不断变化的单元格高度的单元格时,UITableView会滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,它在图像中加载时包含占位符。加载图像时,我调用 reloadRowsAtIndexPaths:withRowAnimation:。此时,单元格会根据图像的大小更改高度。当发生这种情况时,我希望表格视图的内容偏移保持在原位,并且下面的单元格可以进一步向下推,就像你想象的那样。

I have a table view which contains a placeholders while it loads in images. When the image is loaded, I call reloadRowsAtIndexPaths:withRowAnimation:. At this point, the cell changes height, based on the size of the image. When that happens, I want the table view's content offset to remain in place, and for the cells below to be pushed further down, as you might imagine.

效果I相反,滚动视图会滚动回到顶部。我不确定为什么会这样,我似乎无法阻止它。在 reloadRows beginUpdates()之前和 endUpdates()之后> line没有效果。

The effect I'm getting instead is that the scroll view scrolls back to the top. I'm not sure why this is, and I can't seem to prevent it. Putting beginUpdates() before and endUpdates()after the reloadRows line has no effect.

我正在使用 estimatedRowHeight ,因为我的表视图可能有数百个不同高度的行。我也在实现 tableView:heightForRowAtIndexPath:

I am using estimatedRowHeight, as is needed as my table view can potentially have hundreds of rows of different heights. I am also implementing tableView:heightForRowAtIndexPath:.

编辑:我已经设置了一个演示项目来测试这个,诚然,我无法让演示项目重现这种效果。我将继续努力。

I've set up a demo project to test this, and admittedly I can't get the demo project to reproduce this effect. I'll keep working at it.

推荐答案

始终更新主页上的用户界面螺纹即可。所以只需放置

Always update the UI on the main thread. So just place

[self.tableView reloadData];

dispatch_async(dispatch_get_main_queue(), ^{
     //UI Updating code here.
     [self.tableView reloadData];
});

这篇关于当重新加载具有不断变化的单元格高度的单元格时,UITableView会滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆