Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

cell layout is not update when we are try to set image

enter image description hereI have try to download image and set to image view, but cell layout is not calculated correct, but when we are try to scroll tableView then cell layout is correct.

 func setUpCellDataWithImage(indexPath : IndexPath)  {
    let imageNum = String(describing: indexPath.row)
    countryName.text = imageNum
    let url : NSString = (imageUrl + imageNum) as NSString
    if let image = getImageFromUrl(urlString: url) {
      self.imageIcon.image = image
      return
    } else {
      //Download New Image
      self.downloadImageData(url: url)
    }

  }


  func getImageFromUrl(urlString : NSString) -> UIImage?{

    if let image = imageCache.object(forKey: urlString){

      return image
    }
    return nil
  }

  func downloadImageData(url : NSString)  {
    DispatchQueue.global(qos: .background).async {
      ImageDataModel().downloadImageFromUrl(urlString: url) { image in
        //Downloaded Image
        DispatchQueue.main.async {
          self.imageIcon.image = image
          self.imageCache.setObject(image!, forKey: url)
          self.setNeedsLayout()
          self.layoutIfNeeded()
        }
      }
    }
  }

Comments