【问题标题】:Dynamic cell height with SDWebImage使用 SDWebImage 的动态单元格高度
【发布时间】:2018-01-22 16:18:04
【问题描述】:

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

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

我没有忘记:

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
}

表格视图单元格类:

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

【问题讨论】:

    标签: ios sdwebimage


    【解决方案1】:

    你只需要告诉整个tableView 来刷新它的视图。在持有tableView的视图控制器中使用此代码:

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

    使用委托模式告诉 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
        }
    }
    

    【讨论】:

    • 谢谢,这很有效,但它滞后了很多(尤其是当用户滚动到顶部时)。有什么办法吗?
    • @Cesare 之前有滞后吗?如果是,使用时间分析器来检测哪些代码是时间效率低的
    • @Cesare 如果没有,那么我建议你在post 模型中添加一个imageSize 属性,一旦下载了图像,就将其存储在那里。并修改约束处理,如果你已经加载了图像(因此你有imageSize),不要在回调中设置约束,而是直接在setupCell中设置约束,如果是这种情况,那么在回调只设置图像,而不是约束(同样,在这种情况下不要显式刷新表)
    • 不幸的是它没有落后。
    • 所以imageSizeCGSize?另外,由于帖子数组由视图控制器控制,因此不确定如何存储 imageSize 属性。
    猜你喜欢
    • 2014-06-23
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多