簡體   English   中英

具有異步UIImageView的自動布局UITableViewCell不起作用

[英]Autolayout UITableViewCell with async UIImageView not working

我有一個具有基本配置的UITableView tableview單元格僅包含一個UIImageView

查看層次結構:

查看層次結構

我想在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

我看着你的代碼,你想通過自適應單元格高度來適應圖片高度

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
    }

估算的身高代碼也需要刪除

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM