简体   繁体   English

重新加载数据后,我可以在我的 tableView 中保持相同的位置吗?

[英]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.

相关问题 重新加载后迅速保持tableview数据位置 - swift keep tableview data position after reload 为什么分配图像后我的UIButton更改大小和位置? 如何保持相同? - why is my UIButton changing size and position after assigning an image? How can I keep it the same? 它是一种在重新加载tableview后保留contentOffset的方法吗? - Is it a way to keep contentOffset after reloading tableview? 在Swift 2中重新加载数据时,如何保持表格格式不变? - How can I keep the formatting of my table from changing when reloading the data in Swift 2? 完成解析 function 后重新加载 TableView 中的数据 - Reloading Data in TableView after completing parse function 重新加载tableview后没有获取表视图数据 - not getting table view data after reloading tableview 从对象传递数据后,TableView不会重新加载 - TableView is not reloading after data passing from object 从DetailViewController弹出后重新加载TableView数据 - Reloading TableView Data after pop From DetailViewController 在获取异步JSON数据后重新加载tableview - Reloading tableview after fetching asynchronous JSON data 重新加载tableView后无法选择特定的UITableViewCell - Can not select specific UITableViewCell after reloading tableView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM