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?
Asked
Active
Viewed 111 times
0
-
1Possible duplicate of [How to cache images using URLSession in Swift](https://stackoverflow.com/questions/40873685/how-to-cache-images-using-urlsession-in-swift) – Gustavo Vollbrecht Aug 29 '19 at 02:33
-
@Aldo how you can download image and load to image view? – Sagar Bhut Aug 29 '19 at 04:52
-
Are you downloading the image and then showing it in your table or having raw image in assets ? – Abhishek Aug 29 '19 at 06:23
1 Answers
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