【发布时间】: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