由于SDWebImage图像高度而调整表格视图高度

时间:2018-08-14 09:30:40

标签: swift swift4

我正在使用Swift 4,由于这些在线图像的高度,我想获取在线图像并调整表格视图单元格的高度和表格视图的高度。我正在使用SDWebImage来显示在线图像,这是代码:

var images = ["http://styliee.com/images/products/6d9vH1Kk2r.jpeg","http://styliee.com/images/products/6d9vH1Kk2r.jpeg"]

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

    let imageUrl = URLs.productImageUrl + self.product.images[indexPath.row].name
    cell.adImage.sd_setImage(with: URL(string: imageUrl))
    cell.layoutIfNeeded()
    cell.layoutSubviews()
    return cell
}

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

但是它不能正常工作, 有帮助吗?

1 个答案:

答案 0 :(得分:0)

Try This

@IBOutlet var tablViewHight : NSLayoutConstraint!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "imgCell", for: indexPath) as! imgCell

        if let image = UIImage(named:"6d9vH1Kk2r.jpeg") {

            let ratio = image.size.height / image.size.width

            // Make the frame the right size to show the thumbnail
            let cellFrame = CGRect(x: 0, y: 0, width: tableView.contentSize.width, height: tableView.contentSize.width * ratio)
            let imageView = cell.imgShow // image view constraints pin all edges to the cell edges
            imageView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            imageView?.contentMode = .scaleAspectFit
            imageView?.image = image

            tablViewHight.constant = cellFrame.height
        }
        return cell
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
相关问题