Q:

Why can not I set image?

Why can not I set image?
class ViewController:UIViewController,UITableViewDelegate,UITalbeViewDataSource{
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! CustomTableViewCell
    cell.setCell()
    return cell
  }
}

class CustomTableViewCell:UITableViewCell{
  @IBOutlet weak var artworkImage:UIImageView!

  func setCell(){
    let urlStr = "http://is3.mzstatic.com/image/thumb/Music20/v4/d4/6c/af/d46caf98-ff6c-1707-135d-58d6ed9ea6a2/source/500x500bb.jpg"
    let url = URL(strings: urlStr)
    let data = try? Data(contentsOf: url!)

    if let imageData = data {
      self.artworkImage.image = UIImage(data: imageData)
    }
  }
}

I want to display the image of the URL destination in TableView but the image is not displayed.

this image is log.

A:

setCell() should be like this
func setCell(){
    let urlStr = "http://is3.mzstatic.com/image/thumb/Music20/v4/d4/6c/af/d46caf98-ff6c-1707-135d-58d6ed9ea6a2/source/500x500bb.jpg"
    let url = URL(string: urlStr)
    artworkImage.image = UIImage(named: "ic_placeholder")

    DispatchQueue.global(qos: .background).async {
        let data = try? Data(contentsOf: url!)
        DispatchQueue.main.async {
            if let imageData = data {
                self.artworkImage.image = UIImage(data: imageData)
            }
        }
    }
}

Image should be downloaded on background thread otherwise UI will be blocked. After downloading images you need to make changes in Main Thread.
Also better have a placeholder image.

