【问题标题】:Cells inside TableView inside of a CollectionView have incorrect height on first load (Swift 4)CollectionView 内 TableView 内的单元格在首次加载时高度不正确(Swift 4)
【发布时间】:2018-09-14 08:47:53
【问题描述】:

我在几个 CollectionView 单元中有一个 tableView。每个 tableView 占用单元格的完整大小。 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) 加载集合视图

2) 加载 CollectionViewCell

3) 在 CollectionViewCell 中加载 TableView

4) 重新加载 TableView

但是...当它第一次加载时,TableView 的单个单元格的大小会被缩小:

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

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

这是每个 tableView 单元格的外观。

有什么想法吗?

【问题讨论】:

    标签: swift uitableview uicollectionview


    【解决方案1】:

    不确定这是否会有所帮助,但您的单元格似乎有图像。如果我没有在图像视图中放置占位符图像,我会遇到动态高度单元格的问题。

    【讨论】:

    • 尝试添加占位符,但没有成功。不知道是什么原因造成的。我可以重新加载 CollectionView 来解决它很好,但我认为这不是一个理想的解决方案。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-15
      • 2018-06-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多