Swift 4 AlamofireImage with UITableView Cell

后端 未结 10 3305
梦如初夏
梦如初夏 2021-02-20 12:56

I am using AlamofireImages to try to download an image from a server and display it in my table view cell, this is what I got, but my images does not appear, just the textLabel

10条回答
  •  日久生厌
    2021-02-20 13:53

    This helped me:

    Create this function first to help pull down the image. If you're pulling multiple images, you may want to create a new swift file and make this static or just call it in the file that you need it. This way you can refer to it instead of re-writing code:

    static func loadImage(_ imageView: UIImageView,_ urlString: String) {
        let imgURL: URL = URL(string: urlString)!
        URLSession.shared.dataTask(with: imgURL) { (data, respond, error) in
            guard let data = data, error == nil else { return}
    
            DispatchQueue.main.async (execute: {
                imageView.image = UIImage(data: data)  
            })
        }.resume()
    }
    

    Then to pull down the info:

    if let logoName = variable.logo {
            StaticFileName.loadImage(cell.imgVariableInCell, "\(logoName)")
        }
        return cell
    }
    

    Then you're good to go.

提交回复
热议问题