0

I did the following code and is working pretty decent but I am not finding the way to reload the tableView after each image is downloaded. I am using Alamofire to download all the content list data and images.

How I can access to tableView from extension?

If someone can give me any advice I will appreciate so much :)

import UIKit
import Alamofire
import AlamofireImage

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var jsonArray: NSArray?
    var nameArray: Array<String> = []
    var imageURLArray: Array<String> = []

    override func viewDidLoad() {
        super.viewDidLoad()
        downloadDataFromAPI()
    }

    func downloadDataFromAPI(){
        Alamofire.request("http://private-135b4e-solamente.apiary-mock.com/pokemonList") .responseJSON { response in
            if let JSON = response.result.value{
                self.jsonArray = JSON as? NSArray
                for item in self.jsonArray! as! [NSDictionary]{
                    //5.
                    let name = item["name"] as? String
                    let imageURL = item["image"] as? String
                    self.nameArray.append((name)!)
                    self.imageURLArray.append((imageURL)!)
                }
                print(self.nameArray)
                self.tableView.reloadData()
            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.nameArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell:UITableViewCell=UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "mycell")
        cell.textLabel?.text  = nameArray[indexPath.row]
        cell.imageView!.downloaded(from: imageURLArray[indexPath.row])

        return cell
    }

}

extension UIImageView {
    func downloaded(from url: URL, contentMode mode: UIView.ContentMode = .scaleAspectFit) {  // for swift 4.2 syntax just use ===> mode: UIView.ContentMode
        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 downloaded(from link: String, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
        guard let url = URL(string: link) else { return }
        downloaded(from: url, contentMode: mode)
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
solamente
  • 266
  • 2
  • 15
  • 2
    Extending `UIImageView` is not a good practice. Be aware that the image is downloaded again and again when the user scrolls. A better solution is to add the image to the data model and use callbacks in `cellForRow`. If the cell is still visible the image is updated and cached in the model. When the user scrolls the image is taken from the model if available. – vadian Jul 29 '19 at 19:21
  • Thanks Im going to try it – solamente Jul 29 '19 at 19:37

1 Answers1

3

You don't need to reload the table view after each image is downloaded. You need to return the image in the callback in this function func downloaded You can find a similar answer here in objective-c.

Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling

barryjones
  • 2,149
  • 1
  • 17
  • 21