带有图像的动态 UITableView

sas*_*ash 3 uitableview uiimageview autolayout swift

有类似的问题,但没有一个答案对我有用。在动态表中,我想显示具有不同高度的图像。每个单元格都有一个UIImageViewwith,contentMode = .scaleAspectFit所以图像很好地占据了表格的宽度,并根据需要占据了高度。

单元格有 4 个约束: 在此处输入图片说明

表视图控制器:

class TableTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 2
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ImageTableViewCell.self), for: indexPath) as! ImageTableViewCell

        let image = indexPath.row == 0 ? UIImage(named: "1.jpg")! : UIImage(named: "2.jpg")!
        cell.customImageView.image = image

        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明

如您所见,单元格的高度不正确(图像视图顶部和底部的图像视图的红色背景)。我相信这是因为intrinsicContentSize图像视图等于图像大小,这就是为什么单元格的高度计算不正确(不考虑内容模式)。我尝试计算图像的高度并为图像视图添加高度约束:

cell.heightConstraint.constant = cell.frame.width * image.size.height /  image.size.width
Run Code Online (Sandbox Code Playgroud)

但它打破了单元格的内容视图约束。

该项目可以在这里下载>>

Dha*_*esh 5

在你的 ImageTableViewCell.swift

import UIKit

class ImageTableViewCell: UITableViewCell {

    @IBOutlet weak var customImageView: UIImageView!

    internal var aspectConstraint : NSLayoutConstraint? {
        didSet {
            if oldValue != nil {
                customImageView.removeConstraint(oldValue!)
            }
            if aspectConstraint != nil {
                customImageView.addConstraint(aspectConstraint!)
            }
        }
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        aspectConstraint = nil
    }

    func setPostedImage(image : UIImage) {

        let aspect = image.size.width / image.size.height

        aspectConstraint = NSLayoutConstraint(item: customImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: customImageView, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)

        customImageView.image = image
    }
}
Run Code Online (Sandbox Code Playgroud)

进入你TableTableViewController.swiftcellForRowAt方法将是:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ImageTableViewCell", for: indexPath) as! ImageTableViewCell

    let image = imageArr[indexPath.row]
    cell.setPostedImage(image: image!)
    return cell
}
Run Code Online (Sandbox Code Playgroud)

并以imageArr这种方式声明您:

let imageArr = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg")]
Run Code Online (Sandbox Code Playgroud)

您的竞争代码将是:

import UIKit

class TableTableViewController: UITableViewController {

    let imageArr = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg")]
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 2
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ImageTableViewCell", for: indexPath) as! ImageTableViewCell

        let image = imageArr[indexPath.row]
        cell.setPostedImage(image: image!)
        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

将是你的结果。

编辑:

要解决问题,约束一套aspectConstraint优先999aspectConstraint将是:

internal var aspectConstraint : NSLayoutConstraint? {
    didSet {
        if oldValue != nil {
            customImageView.removeConstraint(oldValue!)
        }
        if aspectConstraint != nil {
            aspectConstraint?.priority = 999  //add this
            customImageView.addConstraint(aspectConstraint!)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)