【发布时间】:2018-02-26 10:23:25
【问题描述】:
让我从解释视图层次结构开始。 我使用的是普通的 UITableView,在 UITableViewCell 内有一个高度动态的 collectionView。
我如何在 UITableView 的 willDisplay 单元格中为 UICollectionView 设置数据源和委托:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
guard let collectionCell = cell as? movieListingTableViewCell else { return }
collectionCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
collectionCell.selectionStyle = .none
}
}
collectionView 具有顶部、底部、前导、尾随和高度约束。
然后我在 UITableView 单元子类中使用高度约束并将其设置为 collectionView 高度,
override func layoutSubviews() {
timingCollectionHeight.constant = timingCollection.collectionViewLayout.collectionViewContentSize.height
}
发生的情况是我的单元格没有正确显示,因为有空格。例如,如果有一个 UITableView 单元格的 collectionView 高度很大,那么其他 UITableView 单元格也会以某种方式使用相同的高度。滚动表格后,单元格显示正确。
【问题讨论】:
标签: ios swift uitableview uicollectionview