TableView willDisplayCell called for cells that are not on the screen (tableView pagination)

I had this problem when using UITableView.automaticDimension for cell height. I solved this problem using a higher value for estimatedRowHeight property.

Something like this:

tableView.estimatedRowHeight = 1000

Now willDisplay will be called only for rows which are visible.


As @iWheeBuy said, willDisplayCell does not say that cell is on screen, so you can do it by checking in tableView.visibleCells.

However, even if this function is called when you scroll and it is going to be on screen tableView.visibleCells.contains(cell) will be false.

One approach that works for me is to make a delay, so full code is: (SWIFT 4)

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            if tableView.visibleCells.contains(cell) {
                // load more data
            }
        }
    }