I hva an issue with populating my cells. When scrolling really fast the cells go through 2-3 images before finding the right one. I loaded up an array with a JSON of 1000 items, and used the urls from them to fetch the images with this extension:
extension UIImageView {
func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFill) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() {
self.image = image
}
}.resume()
}
func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFill) {
guard let url = URL(string: link) else { return }
downloadedFrom(url: url, contentMode: mode)
}
This all works great, aside from the issue above. But I was also wondering if there was a better way to load up some of the items in the array, and then append more as I scroll? I should mention, my fetchData function, also contains the append in array function.
Thanks so much in advance.