UITableview在重新加载时滚动到顶部 [英] UITableview Scrolls to Top on Reload
本文介绍了UITableview在重新加载时滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序遇到问题-您可以发布和编辑自己喜欢的地方。发布帖子或编辑特定帖子( UITableViewCell
)后,重新加载 UITableview
。
I am facing problem in my app - you can post and edit your favorite places. After posting a post, or editing a specific post (UITableViewCell
), UITableview
is reloaded.
我的问题是: UITableview
在重新加载后滚动到顶部。但这不是我想要的。我希望我的视图保持在原所在的单元格/视图上。但我不知道该如何处理。
My problem is: the UITableview
scrolls to the top after reloading. But that's not what I want. I want my view to stay on the cell / view where I was. But I don't know how to manage that.
您能帮我吗?
推荐答案
如果Igor的答案正确,您正在使用可动态调整大小的单元格(UITableViewAutomaticDimension)
Igor's answer is correct if you are using dynamically resizable cells (UITableViewAutomaticDimension)
这里是快速3:
private var cellHeights: [IndexPath: CGFloat?] = [:]
var expandedIndexPaths: [IndexPath] = []
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeights[indexPath] = cell.frame.height
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cellHeights[indexPath] {
return height ?? UITableViewAutomaticDimension
}
return UITableViewAutomaticDimension
}
func expandCell(cell: UITableViewCell) {
if let indexPath = tableView.indexPath(for: cell) {
if !expandedIndexPaths.contains(indexPath) {
expandedIndexPaths.append(indexPath)
cellHeights[indexPath] = nil
tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
//tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
}
}
这篇关于UITableview在重新加载时滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文