首页 > 解决方案 > 如何在 UITableView Cell 中为 ImageView 添加大小和位置?

问题描述

我在我的 UITableView 单元格中添加了一张图片:

cell.imageView!.image = UIImage(named: "Default.png")

一切正常,图像显示。现在我想为图像提供一个自定义大小和位置,所以我尝试使用以下代码:

cell.imageView!.frame = CGRect(x: 29, y: 17, width: 14, height: 13)

但无论出于何种原因,它都不起作用。有什么建议吗?

标签: swiftimageimageviewtableviewcell

解决方案


如果你的位置不会改变你可以在你的TableViewCell

class TableViewCell: UITableViewCell {

@IBOutlet weak var imageView: UIImageView!

override func layoutSubviews() {
    super.layoutSubviews()

    self.imageView?.frame = CGRect(x: 12, y: 23, width: 12, height: 100)
}
}

您可以更改框架,tableview(cellForRowAt)但如果框架不会改变它的坏处,因为cellForRowAt在向上滚动或加载单元格时运行。您可以在 UITableViewCell 中定义一次。


推荐阅读