【发布时间】:2018-11-26 04:41:08
【问题描述】:
我正在开发聊天应用程序。
我想添加图片发布功能。
以下为省略代码。
class ImageCell: UITableViewCell {
@IBOutlet private weak var contentImageView: UIImageView!
@IBOutlet private weak var imageViewHeightConstraint: NSLayoutConstraint!
func prepare(url: String) {
contentImageView.kf.setImage(url: url) { image in
let height = self.contentImageView.bounds.width / image.size.width * image.size.height
self.heightConstraint.constant = min(300, height)
self.layoutIfNeeded()
}
}
}
prepare() 与cellForRowAt 的tableView 一起调用
但是TableViewCell的高度变化不大。
我该如何解决?
谢谢。
附言
extension UIViewController: UITableViewDataSource {
ffunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.elements.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath)
cell.prepare(url: elements[indexPath.row].url)
return cell
}
}
【问题讨论】:
-
您能在此处添加您的 Tableview 数据源和委托方法吗?
-
如果我们想要动态的单元格高度,那么需要的图像是
aspect ratio或width -
@HPM 你好,我添加了一个数据源方法!
-
@touyu,我让你添加 Tableview 委托而不是 UICollectionview!。已设置 tableview.estimatedrowheight = #YOUR_ESTIMATED_HEIGHT# 和 tableview.rowheight = .automatic
-
简单的回答没有
heightForRowAt是不可能的
标签: ios swift uitableview tableview