【发布时间】:2020-01-13 14:42:30
【问题描述】:
在我 reloadData() 后,我的 tableView 出现问题。它总是滚动到顶部。我不知道我在哪里搞砸了,因为在保持偏移位置之前它工作得很好。
我使用的是自动尺寸。
var currentCount: Int {
return news.count
}
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 200
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if !fetchingMore && indexPath.row == currentCount - 1 {
beginBatchFetched()
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.estimatedRowHeight
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
private func beginBatchFetched() {
fetchingMore = true
customGetAllNews(serchKey: qKey, tags: tagsAsParameter, cameFromPullToRefresh: false)
}
private func customGetAllNews(serchKey: String?, tags: [String]? = nil,....) {
Helper.getAllNews(page: self.currentPage, key: serchKey........) { (data) in
self.fetchingMore = false
guard let nws = data?.news else {return}
self.currentPage += 1
self.news.append(contentsOf: nws)
GlobalMainQueue.async {
self.tableView.reloadData()
}
}
我还尝试了其他帖子中一些已接受的答案,例如在重新加载之前保存我的表格视图的偏移量,然后设置它,但它不能正常工作,因为我仍然需要滚动一下达到我之前的目标。
另外,我尝试了heightDictionary: [Int : CGFloat] = [:]的方法
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
heightDictionary[indexPath.row] = cell.frame.size.height
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
let height = heightDictionary[indexPath.row]
return height ?? UITableViewAutomaticDimension
}
但是不成功,结果还是一样,跳转到tableview的顶部。
【问题讨论】:
-
您在哪个设备和 iOS 版本上运行此实验?我在 iOS 13 之前的旧 iOS 设备上遇到过这种行为。
-
最新,iOS 13.xxx
标签: ios swift uitableview reloaddata contentoffset