-1

Currently I'm working on an app that involved presenting data in a UITableView. I ran into a problem described below.

Problem:

I performed a delete row operation on the tableview and then called reloadData() on the tableview. While reloading, if I scroll the tableView immediately, there is a jumpy effect observed in the tableview.

Here is the code to delete tableview row,

self.viewModel.data.remove(at: index)
self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .fade)
self.tableView.reloadData()

I want to know how to resolve this issue. This is the same functionality we can observe while hiding a Facebook/Instagram post.

enter image description here

PGDev
  • 23,751
  • 6
  • 34
  • 88
  • [This](https://stackoverflow.com/questions/28244475/reloaddata-of-uitableview-with-dynamic-cell-heights-causes-jumpy-scrolling) other question may help you. Basically, you need to save the height for the cells to avoid the jumpy recalculation of the cells height. – Ángel Téllez Feb 06 '19 at 14:15
  • Implementing tableView:estimatedHeightForRowAtIndexPath: method helped me to resolve similar problem. – tema-orange Feb 07 '19 at 12:11
  • @tema-orange I’ve already implemented this method and still getting the same issue. – PGDev Feb 07 '19 at 12:14
  • Also I mentioned that reloadData seems to be unnecessary there.. – tema-orange Feb 07 '19 at 12:21
  • @tema-orange Removing `reloadData` from the above code didn't make any difference. – PGDev Feb 08 '19 at 06:00
  • Do comment the reason for downvoting as well. – PGDev Jun 18 '19 at 08:01

2 Answers2

2

Calling reloadData reloads the entire table view, which you don't need. To animate deletion of the single row use beginUpdates and endUpdates

self.tableView.beginUpdates()
self.viewModel.data.remove(at: index)
self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .fade)
self.tableView.endUpdates()
mag_zbc
  • 6,801
  • 14
  • 40
  • 62
0

Enqueue reloading in main thread and also you don't need to reload whole table unless you have changed a lot of data source, just reload section.

self.viewModel.data.remove(at: index)
self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .fade)
OperationQueue.main.addOperation {
    tableView.reloadSections(IndexSet(integer: 0), with:.automatic)
}