[英]Can I keep the same position in my tableView after reloading the data?
I'm having issues with my tableView after I reloadData().在我 reloadData() 后,我的 tableView 出现问题。 It always scrolls to the top.它总是滚动到顶部。 I don't know where I've messed it up since before it worked very fine maintaining the offset position.我不知道我在哪里搞砸了,因为之前它可以很好地保持偏移位置。
I'm using an automatic dimension.我正在使用自动尺寸。
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()
}
}
I've also tried some accepted answers from other posts like saving the offset of my table view right before the reload and after that set it, but it doesn't work as it should, because I still need to scroll a bit to get to the point I was before.我还尝试了其他帖子中一些已接受的答案,例如在重新加载之前和之后保存我的表格视图的偏移量,但它无法正常工作,因为我仍然需要滚动一下才能到达我之前的观点。
Also, I've tried the method with the heightDictionary: [Int : CGFloat] = [:]
另外,我已经尝试过使用heightDictionary: [Int : CGFloat] = [:]
的方法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
}
but unsuccessfully, the result was the same, jumps to the top of the tableview.但是不成功,结果还是一样,跳转到tableview的顶部。
@Mohamed, please have a look with the below code and put this inside your mainqueue instead of reloading the data. @Mohamed,请查看以下代码并将其放入主队列中,而不是重新加载数据。
Please check and let me know if this works for you.请检查并让我知道这是否适合您。
GlobalMainQueue.async { self.tableView.reloadData() } GlobalMainQueue.async { self.tableView.reloadData() }
UIView.setAnimationsEnabled(false)
self.tableView.beginUpdates()
self.tableView.reloadSections(IndexSet(index: 1) as IndexSet, with:
UITableViewRowAnimation.none)
self.tableView.endUpdates()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.