【问题标题】:Empty space in UIImageViewUIImageView 中的空白区域
【发布时间】:2015-09-08 10:30:16
【问题描述】:

我有一个表格视图,其中每个单元格都有一个图像。

图片来自不同的尺寸。所以当我制作一个单元格时,我必须给 UIImageView 一个宽度和高度的约束。

现在我想要的是:当图像加载到单元格的 UIImageView 中时,UIImageView 的大小应该根据图像本身进行调整。

我在过去 2 天进行了试验,但无法修复它。由于限制,表格中的单元格一直有空格。

我用来填充单元格的代码:

class ProductTableViewCell: UITableViewCell {


@IBOutlet weak var productImageView: UIImageView!
@IBOutlet weak var productTitleLabel: UILabel!
@IBOutlet weak var productDescriptionLabel: UILabel!
var orgframe = UIImageView()


func configureCellWith(product: Product){
    orgframe.frame.size = CGSizeMake(400, 600)
    productImageView.image = product.image
    productDescriptionLabel.text = product.description
    productTitleLabel.text = product.title

    var urlstring = product.imageURL

    ImageLoader.sharedLoader.imageForUrl(urlstring as String, completionHandler:{(image: UIImage?, url: String) in
        //self.productImageView.frame.size = (image?.size)!

        self.productImageView.image = image!


    })


}


}

UIImageview 的约束:

【问题讨论】:

    标签: xcode swift uitableview tableview tableviewcell


    【解决方案1】:

    给 productImageView 一个固定的高度约束,并将其连接到一个 IBOutlet。然后,当您准备好加载图像时,您可以这样做:

    self.productImageHeightConstraint.constant = image.size.height
    self.view.layoutIfNeeded()
    

    您需要确保将周围的视图/标签固定到图像视图的边缘,以便它们根据图像大小自行排列。

    【讨论】:

    • 我做到了,它有帮助但是:所有元素都向下移动,除了分隔符。所以单元格的大小没有适应它的新大小。我该怎么做?
    • 在 heightForRowAtIndexPath 中,根据图像高度返回单元格的高度(如果图像尚未下载,则返回一些默认高度)。
    • 我返回一个默认高度,但之后我想准确地更新单元格的高度以删除所有空格。所以高度必须尽可能低。我可以使用什么命令,从我配置单元格的 producttableviewcell.swift 文件中
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    相关资源
    最近更新 更多