我正在开发聊天应用程序。
我想添加图片发布功能。
以下为省略代码
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()
被 tableView
的 cellForRowAt
调用
但是,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
}
}
最佳答案
在单元格中的 UIImageView 上放置前导、顶部、底部和右侧约束。
heightForRow方法返回图片的高度:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return image.size.height
}
关于ios - 如何根据UIImageView的高度来估算UITableViewCell的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53474871/