Learn Courses My Dashboard

Image View not updating properly

Hey,

Can someone pls tell me why the following image view is updating wrong

I am trying to update the images fetched from a network call but for some reason the returned images are overwritten over the placeholder image I provided.

Here is the code I used for updating the cell

func configure(cell: ItemCell, forItemAt indexPath: IndexPath) {
        
        let item = items[indexPath.row]
        
        DispatchQueue.main.async {
            
            
            // set cell.titleLabel to the item's name
            cell.titleLabel.text = item.name
            
            // set cell.detailLabel to the item's artist
            cell.detailLabel.text = item.artist
            
            // set cell.itemImageView to the system image "photo"
            // cell.itemImageView.image = UIImage(systemName: "photo")
        }
        
        // initialize a network task to fetch the item's artwork
        let task = URLSession.shared.dataTask(with: item.artworkURL) { (data, response, error) in
            if let data = data,
               let image = UIImage(data: data) {
                // if successful, use the main queue capture the cell, to initialize a UIImage, and set the cell's image view's image to the
                DispatchQueue.main.async {
                    cell.imageView?.image = image
                }
                
                
            }
            else if let error = error {
                print(error)
                cell.itemImageView.image = UIImage(systemName: "photo")
            }
        
        
        }
        
        task.resume()
        

I use this function in tableview(cellForRowAt) method to modify the cell.

thanks for taking the time to help

its simply because you placed it in the default imageView provided by the tableviewcell

when it should be your itemImageView?.image

the default tableviewcell provided by swift actually has a built in imageview, titlelabel, descriptionlabel, and accessoryview (another view at the right side, usually used for icons or images)

1 Like