我必须添加一个UICollectionView
里面一个UITableViewCell
.
The collectionView
可以有不同数量的项目。
所以collectionView
应在内部适当调整tableView
.
我已经在我的项目中实现了这个:https://github.com/vishalwaka/DynamicCollectionViewInsideTableViewCell https://github.com/vishalwaka/DynamicCollectionViewInsideTableViewCell
就我而言,单元格的高度在单元格的高度之后没有进行调整collectionView
is set.
我怎样才能设置collectionView
不可滚动并显示其中的所有内容tableviewcell
.
创建一个collectionView
inside UITableViewCell
具有前导、尾随、顶部、底部、高度约束,如下所示:
现在,在你的tableViewCell
类做一个NSKeyValueObserver
并将其添加到UICollectionView
's 内容大小属性并将 contentSize 更改分配给 collectionView 的高度约束。
class FormCollectionTableViewCell: UITableViewCell{
var collectionViewObserver: NSKeyValueObservation?
override func awakeFromNib() {
super.awakeFromNib()
addObserver()
}
override func layoutSubviews() {
super.layoutSubviews()
layoutIfNeeded()
}
func addObserver() {
collectionViewObserver = collectionView.observe(\.contentSize, changeHandler: { [weak self] (collectionView, change) in
self?.collectionView.invalidateIntrinsicContentSize()
self?.collectionViewHrightConstarint.constant = collectionView.contentSize.height
self?.layoutIfNeeded()
})
}
deinit {
collectionViewObserver = nil
}
}
在你的 viewController 类中使用以下代码:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.reloadData()
}
现在,您的表视图将具有具有动态高度的集合视图。不要忘记禁用滚动UICollectionView
并调整其流量。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)