UITableView dynamic cell heights only correct after some scrolling when resizing cell according to uiimage view

242 views Asked by At

enter image description here

As you can see in the 1st Image . No text visible in UITableviewCell. After I scroll it appears and resize the cell. I want to resize cell according to UIImage. I am fetching image  from API using SDWebImage. I am putting code I have done in UITableView Cell for row method .

cell.imgViewSlide.sd_setImage(with: URL(string: 
  self.arrSliderImage[indexPath.row]), placeholderImage: UIImage(named: "HomeSliderImage"), options: .continueInBackground) { (image, error, type, url) in

let screenBounds = UIScreen.main.bounds

let pixelWidth = Int((image?.size.width)!)

let pixelHeight = Int((image?.size.height)!)


let height = ((pixelHeight  * Int(screenBounds.width)) / pixelWidth)                                
cell.heightImage.constant = CGFloat(integerLiteral: height)                                  
                                }

I have applied plenty of solution but not working correctly.

enter image description here

1

There are 1 answers

0
Samarth Kejriwal On

First of all, as you are fetching the Images from dynamic URL using SDWebImage, Initially you need to set a static height for the image of your tableView Cell in the storyboard.

Define a constraint Outlet for this image height in your cell, and then you should define the height of the image according to the aspect Ratio.

cell.imgViewSlideHeight.constant = CGFloat((aspectRatio) * Double(self.frame.width))

Here self is the UITableViewCell reference and imgViewSlideHeight is the cell Image Height Constraint.