1

I have this tableview that loads new data on WillDisplayCell delegate method when it reaches the last cell. The issue is when the user scrolls down and this method is called, inserting new cells makes the tableView scrolls up to show the new added cells and that produces a very weird animation.

Now, I can simply call tableView.reloadData() to avoid this, but this method reloads everything. and I need to reload only the new non visible cells as I'm giving them a custom animation in WillDisplayCell method.

Any suggestion to implement a seamless UITableView pagination with animated cells?

Lal Krishna
  • 12,476
  • 4
  • 53
  • 65
Zakaria
  • 910
  • 2
  • 11
  • 26

2 Answers2

0

Was having this problem myself recently while trying to implement a TikTok like scrolling experience. The secret sauce is to implement

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat

and return the cell height, even if the tableview height has a known fixed number

I had no clue that estimated cell heights impacted row insertion but it seems to be without question true.

(This was my experince fixing the problem for an iOS13+ app)

bitwit
  • 2,393
  • 2
  • 25
  • 32
-1

This solution works for me, but it really depends on your UI. Probably there is something more in your case:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == data.count - 5 {
        let time = DispatchWallTime.now() + DispatchTimeInterval.seconds(1)
        DispatchQueue.main.asyncAfter(wallDeadline: time) {
            let indexes = 33...55
            self.data.append(contentsOf: indexes)
            let dataCount = self.data.count

            let paths:[IndexPath] = ((dataCount-indexes.count)...(dataCount-1)).flatMap({ index -> IndexPath in
                return IndexPath(row: index, section: 0)
            })
            self.tableView.insertRows(at: paths, with: .right)
        }
    }
}

And here I'm mocking data get, but probably you will have to implement protection to block any call while the data is still loading.

When I test, there is no strange scrolling from the table view.

Lachezar Todorov
  • 762
  • 7
  • 20