【问题标题】:Automatic table height with collectionView content - Swift带有collectionView内容的自动表格高度 - Swift
【发布时间】:2019-11-12 08:09:44
【问题描述】:
层次结构如下所示:
集合视图内容由 ajax json 加载。
我希望表格在加载数据后调整高度。
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
【问题讨论】:
标签:
swift
xcode
uitableview
swift3
uicollectionview
【解决方案1】:
首先你需要像这样动态改变 UICollectionView
import UIKit
class DynamicCollectionView: UICollectionView {
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
}
override var intrinsicContentSize: CGSize {
return self.collectionViewLayout.collectionViewContentSize
}
}
并将这个 collectionView 放在 UITablaViewCell 中。之后你应该写在你的 viewDidLoad() 方法中
tableView.estimatedRowHeight = 44 //height which one you want, it does not matter
tableView.rowHeight = UITableView.automaticDimension
这就是全部。我希望这将有助于解决您的问题。