【发布时间】:2020-11-04 14:01:42
【问题描述】:
我有一个包含 7 种 UITableViewCells 的垂直列表。其中之一在单元格内包含一个 UITableView。
我的要求是主tableview应该根据单元格的内部tableview contentSize自动调整单元格。那个 os 内部的 tableview 将显示它的全长并且滚动将被关闭。
This approach 工作正常,但仅适用于带有 tableview 的单元格。当我在内部 tableview 上方引入 UIImageView(带有异步加载图像)时,单元格的总高度略小于其内容的实际高度。所以内部的tableview被从底部切掉了。
这是错误的表示。
我正在根据宽度设置UImageView的高度以正确缩放:
if let media = communityPost.media, media != "" {
postImageView.sd_setImage(with: URL(string: media), placeholderImage: UIImage(named: "placeholder"), options: .highPriority) { (image, error, cache, url) in
if let image = image {
let newWidth = self.postImageView.frame.width
let scale = newWidth/image.size.width
let newHeight = image.size.height * scale
if newHeight.isFinite && !newHeight.isNaN && newHeight != 0 {
self.postViewHeightConstraint.constant = newHeight
} else {
self.postViewHeightConstraint.constant = 0
}
if let choices = communityPost.choices {
self.datasource = choices
}
self.tableView.reloadData()
}
}
} else {
self.postViewHeightConstraint.constant = 0
if let choices = communityPost.choices {
datasource = choices
}
tableView.reloadData()
}
而内表视图是 UITableView 的子类:
class PollTableView: UITableView {
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return self.contentSize
}
override var contentSize: CGSize {
didSet{
self.invalidateIntrinsicContentSize()
}
}
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
}
}
主表视图设置为使用自动维度调整大小:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeights[indexPath] = cell.frame.size.height
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return cellHeights[indexPath] ?? UITableView.automaticDimension
}
似乎无法理解出了什么问题。任何帮助表示赞赏。 谢谢。
【问题讨论】:
标签: ios swift uitableview autoresize