UITableViewCell高度问题

huangapple go评论93阅读模式
英文:

UITableViewCell Height Issue

问题

我实际上正在尝试使图像视图的高度动态化。

我已经尝试过 UITableViewAutomaticDimension

在单元格类中,我已根据纵横比约束设置了图像的动态高度。

英文:

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

答案1

得分: 1

你可以使用UITableViewAutomaticDimension来实现基于图像约束的单元格的动态高度。

但是,图像的高度和宽度可以帮助你实现这一目标。关于这个问题有很多帮助可在以下答案中找到:

https://stackoverflow.com/questions/32281886/dynamic-uiimageview-size-within-uitableview

英文:

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:

https://stackoverflow.com/questions/32281886/dynamic-uiimageview-size-within-uitableview

答案2

得分: 0

  1. 确保你在Storyboard中设置了从顶部到底部的约束。
  2. 将缩略图图像保存为数据到本地存储(我正在使用Core Data)。
  3. 使用缩略图图像,计算图像的纵横比。
  4. 自定义heightForRowAt方法中的行高,如您所需。
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
    // 自定义以获得所需的输出
    if newH > 500 {
        newH = 500
        // 最大高度为500
    }
    if newH < 100 {
        newH = 100
        // 最小高度为100
    }
    return newH
}

如果本地存储中的缩略图图像被删除,那么将显示一个占位图像。您可以自定义newH变量以获得所需的输出。

英文:
  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) -&gt; 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 == &quot;DeletedMedia&quot; {
            newh = 100
        }
        if entity.fileStatus == nil{
            newh = 0.0
        }
        print (&quot;newH &quot; , newh)
        return newh
    }
    func getImageHeight(image : UIImage, h : CGFloat, w : CGFloat) -&gt; 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 &gt; 500){
            newH = 500
           //maximum height 500
        }
        if(newH &lt; 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

huangapple
  • 本文由 发表于 2020年1月3日 14:18:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定