0

I have an UITableView that loads cells that have heavy images and other animations, it works fine but it looks slow since when scrolling up or down it is renamed cellForRowAt what it does reloads images and animations Is there a way to avoid this if those cells had already been loaded before?

Aldo
  • 41
  • 4

1 Answers1

0

Just move your animation logic to willDisplayCell delegate and for animating only for the first time I've handled using a flag.

Code Sample:

let popularLinks = ["One","Two","Three","Four","Five"]
var isAnimated = false

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

        var lastInitialDisplayableCell = false

        if popularLinks.count > 0 && !isAnimated {
            if let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows,
                let lastIndexPath = indexPathsForVisibleRows.last, lastIndexPath.row == indexPath.row {
                lastInitialDisplayableCell = true
            }
        }

        if !isAnimated {

            if lastInitialDisplayableCell {
                isAnimated = true
            }

            cell.transform = CGAffineTransform(translationX: 0, y: cell.bounds.height/2)
            cell.alpha = 0

            UIView.animate(withDuration: 0.5, delay: 0.05*Double(indexPath.row), options: [.curveEaseInOut], animations: {
                cell.transform = CGAffineTransform(translationX: 0, y: 0)
                cell.alpha = 1
            }, completion: nil)
        }
    }
Hari Krishnan
  • 202
  • 1
  • 5