swift - 带有图像的动态 UITableView

标签 swift uitableview uiimageview autolayout

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

Cell 有 4 个约束: enter image description here

TableView Controller :

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
    }
}

结果:

enter image description here

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

cell.heightConstraint.constant = cell.frame.width * image.size.height /  image.size.width

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

工程可以下载here>>

最佳答案

在你的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
    }
}

在您的 TableTableViewController.swift 中,您的 cellForRowAt 方法将是:

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
}

并以这种方式声明您的imageArr:

let imageArr = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg")]

你的竞争代码将是:

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
    }
}

THIS将是你的结果。

编辑:

要解决约束问题,请将 aspectConstraint 优先级设置为 999 并且 aspectConstraint 将是:

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

关于swift - 带有图像的动态 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42163132/

相关文章:

ios - UIImageView 的图像加载最多需要 10 秒

ios - 获取没有滚动条的UITableView的高度

iphone - 如何将二进制数据转换为 NSData

php - Alamofire 下载文件存在进度问题

ios - 在 Realm 数据库中保存数组值

ios - cellForRowAtIndexPath 中 if 条件中的多个 return 语句抛出 "Control may reach end of non-void function"错误

ios - 按下按钮时如何更改角标(Badge)编号?

iphone - 如何更改 UIImageView alpha onTouchMoved

swift - 无法使用类型为 'Dictionary<String, AnyObject>' 的索引下标类型为 'String' 的值

swift - map 框中路线的多色多段线