1

I'm having issues with my tableView after I reloadData(). 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] = [:]

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.

Mohamed Lee
  • 267
  • 1
  • 3
  • 18

1 Answers1

3

@Mohamed, please have a look with the below code and put this inside your mainqueue instead of reloading the data.

Please check and let me know if this works for you.

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()
Vikash Sinha
  • 869
  • 1
  • 7
  • 21
  • 1
    looks like it's working. But can you briefly explain to me why do I have to use UIView.setAnimationsEnabled(false) – Mohamed Lee Jan 13 '20 at 15:05
  • 1
    @MohamedLee, this set the view animation to false so by default, the tableview while reloading the data, tableview shows the animation and by setting the UIView.setAnimationsEnabled(false) remove the animation which you don't want to show during reloading. Hope you find the solution. Make sure, if you are using the animation anywhere in the project, set this UIView.setAnimationEnabled to true to work with animations everywhere. Just keep this false where you don't want to show animations. – Vikash Sinha Jan 13 '20 at 15:10
  • By doing this my ```UIRefreshControl``` has no more animation – Mohamed Lee Jan 13 '20 at 15:10
  • So, so you have to set this property true when tableview gets reloaded. – Vikash Sinha Jan 13 '20 at 15:12
  • the thing is if I let it to true my tableview has some strange animations, else , with this on false it seems to work fine – Mohamed Lee Jan 13 '20 at 15:13
  • 1
    After endUpdates, set the property to true in your mainQueue block and again when the tableview tries to reload, View animation property is set to false, so it will behave fine and after the endupdates, again set this to true – Vikash Sinha Jan 13 '20 at 15:15
  • well this helps me, let's say only for pagination, for the rest of things like when I'm saving a bookmark and reading news and I'm reloading the indexPath it still acting strange – Mohamed Lee Jan 13 '20 at 15:38
  • Just you need to check where you want this property to apply as false or true. make a property which will reload tableview on the basis of if you want this animation or not. – Vikash Sinha Jan 13 '20 at 15:42