具有异步UIImageView的自动布局UITableViewCell不起作用

时间:2019-09-04 20:33:47

标签: ios swift uitableview autolayout

我有一个UITableView,具有基本的配置。表格视图单元格仅包含一个UIImageView

查看层次结构:

View Hierarchy

我想在UIImageView中展示不同尺寸的图像。 UIImageView的宽度需要固定为可变高度。

“自动版式”应该能够自动调整单元格的大小,但是当异步下载图像时,“自动版式”不起作用。

这是我的cellForRowAt indexPath

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

    let item = array[indexPath.row]
    let url = URL.init(string: item["imageUrl"] as! String)!

    cell.myImage.image = nil
    DispatchQueue.global().async {
        let data = try! Data.init(contentsOf: url)
        DispatchQueue.main.async {

            let image = UIImage.init(data: data)!
            if let cell = tableView.cellForRow(at: indexPath) as? Cell, cell.myImage.image == nil {

                let multiplier = cell.myImage.bounds.width / image.size.width
                cell.heightConstraint.constant =  multiplier * image.size.height

                cell.myImage.image = image

                cell.layoutIfNeeded()
            }
        }
    }

    return cell
}

我在GitHub上有一个示例项目设置来演示该问题。 https://github.com/RishabhTayal/AutoTableViewPOC

1 个答案:

答案 0 :(得分:0)

我看着您的代码,您想通过自适应单元格高度来调整图片高度

self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 1000

图片限制:

Trailing Space to: SuperView
Leading Space to: SuperView
Bottom Space to: SuperView
Top Space to: SuperView
Height Equals:199.5

日志打印将显示太多警告

  

请注意,func heightForRow总是先执行,然后再执行func cellForRowAt

如果您请求func cellForRowAt中的图片,则应在获取高度后再次刷新单元格。我还将删除垂直约束Bottom Space to: SuperView

我下载了Kingfisher,并使用了一个属性来保存下载的图像。

var urlImages = [String : UIImage]()

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

        let item = array[indexPath.row]
        let urlString = item["imageUrl"] as! String
        let url = URL.init(string: urlString)!

        if let cacheImage = urlImages[urlString] {
            cell.myImage.image = cacheImage
            let height = self.view.bounds.size.width * (cacheImage.size.height / cacheImage.size.width)
            cell.imageHeightConstraint.constant = height
        }else{
            cell.myImage.kf.setImage(with: url, placeholder: nil, options: .none, progressBlock: nil) { result in

                switch result{
                case .success(let value):
                    let image = value.image
                    self.urlImages[urlString] = image

                    let height = self.view.bounds.size.width * (image.size.height / image.size.width)
                    cell.imageHeightConstraint.constant = height

                    tableView.reloadData()

                case .failure(let error):
                    print("error \(error)")
                }
            }
        }

        return cell
    }

刷新将首先执行func heightForRowAt,现在这样

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        let item = array[indexPath.row]
        let urlString = item["imageUrl"] as! String
        if let image = urlImages[urlString] {
            let height = self.view.bounds.size.width * (image.size.height / image.size.width)
            return height
        }
        return 44
    }

估计的身高代码也需要删除