-1

I'm actually trying to make the image view height dynamic

I have tried UITableViewAutomaticDimension

in the cell class I have set the image's dynamic height based on the aspect constraint

  • Can you show us what you tried so far in your code and constraints? – Teetz Jan 03 '20 at 06:22
  • I have tried UITableViewAutomaticDimension the image is showing with dynamic height as expected but the cell height is not getting updated @Teetz – Fazal rahman Puliyanchali Jan 03 '20 at 06:24
  • post your code please – Jawad Ali Jan 03 '20 at 06:25
  • 2
    I guess we need some more informations in order to help you. Show some of your ViewController's code and some constraints you set. – Teetz Jan 03 '20 at 06:25
  • I have posted the code. – Fazal rahman Puliyanchali Jan 03 '20 at 06:43
  • There is some confusing stuff going on here. I will cover just a few (only ViewController): 1. Instead of using `dequeueReusableCell(withIdentifier:)` you should use `dequeueReusableCell(withIdentifier:for:)` in `cellForRowAt` 2. Why are you returning `UITableViewCell()` in `cellForRowAt`? 3. You don't need to implement `tableView:willDisplayCell:forRowAtIndexPath:` - this is not how dynamic height works. – Teetz Jan 03 '20 at 07:09
  • I have multiple cell types in my cellforrow (just posted a purticular cell's code only) – Fazal rahman Puliyanchali Jan 03 '20 at 10:27

2 Answers2

1

Well, you can't achieve the dynamic height of cell with UITableViewAutomaticDimension on the basis of image's constraints.

BUT image's height and width can help you in this. There is a plenty of help regarding this issue is available in following answer:

Dynamic UIImageView Size Within UITableView

Rameez
  • 412
  • 1
  • 5
  • 16
0
  1. make sure you have set top-bottom constraints in storyboard.
  2. save the thumbnail image as data to local storage (I'm using core data)
  3. using the thumb image, calculate the aspect of the image
  4. customise the row height as you want in heightForRowAt method.
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let entity = self.fetchedResultController.object(at: indexPath as IndexPath) as! Message
        var newh:CGFloat = 100.00

        if let fileThumbnails =  entity.file_thumbnails as? NSArray{
            if fileThumbnails.count != 0{
                fileThumbnails.map { (thumbNail) in
                    newh = self.getImageHeight(image:UIImage(data: thumbNail as! Data)! , h: UIImage(data: thumbNail as! Data)!.size.height , w: UIImage(data: thumbNail as! Data)!.size.width)
                }

            }

        }

        if entity.fileStatus == "DeletedMedia" {
            newh = 100
        }
        if entity.fileStatus == nil{
            newh = 0.0
        }
        print ("newH " , newh)
        return newh
    }
    func getImageHeight(image : UIImage, h : CGFloat, w : CGFloat) -> CGFloat {
        let aspect = image.size.width / image.size.height
        var newH = (messagesTV.frame.size.width * 0.6) / aspect
        // customise as you want in newH
        if(newH > 500){
            newH = 500
           //maximum height 500
        }
        if(newH < 100){
            newH = 100
            //minimum height = 100
        }
        return newH
    }

if the thumb image is deleted in local storage then a placeholder image will be shown. you can customise the newHvariable to get the desired output