CollectionView内的TableView内的单元格在第一次加载时具有不正确的高度(Swift 4)

时间:2018-04-04 17:24:52

标签: swift uitableview uicollectionview

我在几个CollectionView单元中有一个tableView。每个tableView占用单元格的完整大小。 CollectionView是水平滑动的。

的CollectionView:

collectionView.frame = .zero
collectionView.backgroundColor = UIColor.clear
collectionView.dataSource = self as UICollectionViewDataSource
collectionView.delegate = self as UICollectionViewDelegate
collectionViewLayout.scrollDirection = .horizontal
collectionViewLayout.minimumLineSpacing = 0.0
collectionViewLayout.minimumInteritemSpacing = 0.0
collectionViewLayout.estimatedItemSize = CGSize(width: view.frame.width, height: view.frame.height)
collectionView.collectionViewLayout = collectionViewLayout
collectionView.isPagingEnabled = true
collectionView.register(collectionViewCell.self, forCellWithReuseIdentifier: collectionViewCellID)
collectionView.translatesAutoresizingMaskIntoConstraints = false

collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

CollectionView单元格内的TableView:

tableView.delegate = self
tableView.dataSource = self
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(tableViewCell.self, forCellReuseIdentifier: "tableViewCell")
tableView.register(tableViewCell.self, forCellReuseIdentifier: "emptyTableViewCell")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 105

tableView.topAnchor.constraint(equalTo: topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true

tableView在CollectionView的cellForItemAt下重新加载,并具有自定义单元格。自定义tableView单元格包含几个小视图,这些视图被添加到名为' cellView'的单个视图中。它占据了整个细胞的大小:

let cellView = UIView()
addSubview(cellView)
cellView.translatesAutoresizingMaskIntoConstraints = false
cellView.topAnchor.constraint(equalTo: topAnchor).isActive = true
cellView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
cellView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
cellView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

所以......它像这样加载:

1)加载CollectionView

2)加载CollectionViewCell

3)在CollectionViewCell中加载Ta​​bleView

4)重新加载TableView

但......当它第一次加载时,TableView的各个单元格的大小减少了:

tableView - incorrect height

单元格具有正确高度的唯一方法是整个CollectionView 重新加载一旦发生这种情况,一切都会正确显示。

我认为这可能与estimatedRowHeight有关,但我对此进行了调整而没有任何影响。就像tableView最初将自定义单元格的大小设置为典型的非自定义单元格的大小一样。第一次加载时的单元格。

这就是每个tableView单元格看起来的样子。

有什么想法吗?

enter image description here

2 个答案:

答案 0 :(得分:0)

不确定这是否有帮助,但看起来你的细胞有图像。如果我没有在图像视图中放置占位符图像,我就会遇到动态高度单元格的问题。

答案 1 :(得分:0)

我最终在viewDidAppear中重新加载tableView(而不是collectionView),但它只运行一次(当应用程序启动时)。这解决了这个问题。

This link有一些关于tableView单元格动态高度的有趣想法,但在我的特定情况下没有任何效果。