ios - SDWebImage 的动态单元格高度

标签 ios sdwebimage

我有一个 TableView 和一个带有 ImageView 的 TableView 单元格。我希望它们具有固定宽度但具有动态高度(取决于来自服务器的图像)。

我正在使用 SDWebImage 下载和设置图像,但表格 View 单元格变得非常奇怪。

我没有忘记:

postTableView.estimatedRowHeight = UITableViewAutomaticDimension
postTableView.rowHeight = UITableViewAutomaticDimension

cellForRow 方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! TableViewCell
    let post = postArray[indexPath.row]
    cell.setupCell(with: post)
    return cell
}

表格 View 单元类:

class TableViewCell: UITableViewCell {

    @IBOutlet weak var postTitle: UILabel!
    @IBOutlet weak var postSource: UILabel!
    @IBOutlet weak var postChart: UIImageView!

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

    override func awakeFromNib() {
        super.awakeFromNib()
        selectionStyle = UITableViewCellSelectionStyle.none
    }

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

    func setupCell(with post: Post) {
        postTitle.text = post.title
        postSource.text = post.source
        let tempImageView = UIImageView()
        tempImageView.sd_setImage(with: URL(string: post.chartURL!), placeholderImage: UIImage(named: "placeholder.png")) { (image, error, cache, url) in
            if let image = image {
                self.setCustomImage(image: image)
            }
        }
    }

    func setCustomImage(image: UIImage) {
        let aspect = image.size.width / image.size.height
        let constraint = NSLayoutConstraint(item: postChart, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: postChart, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)
        constraint.priority = UILayoutPriority(rawValue: 999)
        aspectConstraint = constraint
        postChart.image = image
        setNeedsLayout()
    }
}

最佳答案

你只需要告诉整个 tableView 刷新它的 View 。在持有 tableView 的 View Controller 中使用此代码:

func refreshTableView() {
    self.tableView.beginUpdates()
    self.tableView.setNeedsDisplay()
    self.tableView.endUpdates()
}

使用委托(delegate)模式告诉 viewController 刷新 tableView,例如:

func setCustomImage(image: UIImage) {
    let aspect = image.size.width / image.size.height
    let constraint = NSLayoutConstraint(item: postChart, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: postChart, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)
    constraint.priority = UILayoutPriority(rawValue: 999)
    aspectConstraint = constraint
    postChart.image = image

    // call this to refresh table
    delegate.refreshTableView()
}

课后:

class Post {
    var title : String?
    var source : String?
    var chartURL : String?
    var postChart: UIImage?
    var category : String?
    var rank : CGFloat?
    var imageSize: CGSize?

    init(data: NSDictionary) {
        title = data["title"] as? String
        source = data["source"]as? String
        chartURL = data["chartURL"] as? String
        postChart = data["postChart"] as? UIImage
        category = data["category"] as? String
        rank = data["rank"] as? CGFloat
    }
}

关于ios - SDWebImage 的动态单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48385949/

相关文章:

ios - 在 iOS 中计算 hh.mm.ss 格式的时间间隔

ios - 核心数据自定义迁移

ios - 排序 Mapbox 注释

ios - 当我使用 SDWebImage 时滚动不正确

iphone - iPad3拆分键盘时出现灰线问题

ios - iOS客户端如何获取Youtube API中的Client Secret?

ios - 从 SDWebImage 中删除图像缓存

ios - SDWebImage 线程占用 CPU

ios - 无法设置完成 block swift 2.0 w/SDWebImageCompletionBlock