【问题标题】:Resizing table view height due to SDWebImage image height由于 SDWebImage 图像高度而调整表格视图高度
【发布时间】:2018-08-14 09:30:40
【问题描述】:

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

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

【问题讨论】:

  • 如果您从后端获取图像,则从后端获取图像的比例并根据比例调整表格视图单元格的高度
  • 请展示您的 imageView 的约束

标签: swift swift4


【解决方案1】:
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
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2014-10-01
    • 2017-03-10
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多