SDWebImage的动态单元格高度

时间:2018-01-22 16:18:05

标签: ios sdwebimage

我有一个表格视图和表格视图单元格,上面有图像视图。我希望它们具有固定宽度但具有动态高度(取决于从服务器进入的图像)。

我正在使用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()
    }
}

1 个答案:

答案 0 :(得分:4)

您只需告诉整个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
    }
}