swift - 由于 SDWebImage 图像高度而调整表格视图高度
问题描述
我正在使用 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
}
但它不能正常工作,有什么帮助吗?
解决方案
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
}
推荐阅读
- c# - 如何更改父视图模型的属性
- youtube-analytics-api - 通过内容管理器帐户的客户端 ID 访问 youtube 分析 API
- django - 模板中foreginKey的Django问题
- angular - Angular Flex 布局 Pinterest 布局
- jq - jq如何将数组对象合并为单个对象
- email - .ics 托管文件的深层链接未在 Outlook Mobile 中打开
- php - 在 Laravel 中转换日期时间格式
- css - 如何在这个 Wordpress 主题中获得可滚动的左侧菜单
- three.js - THREE.js 中的 TriPlanarMapping
- angular - Angular 9 组件测试:找不到名称“ngbNav”的导出