UITableView可以在里面滚动UICollectionView吗?
我有以下结构……
我将两个集合视图包装到tableview中
一个在tableview标题(Collection1)中,另一个在tableview第一行(Collection2)中。
所有function都很好(两个集合视图)。
只是…
当我在Collection2中向上滚动时,Collection1将不会向上滚动,因为我只滚动collectionViews而不是tableview。
它只在我在Collection1中滚动时一起滚动。
是否有可能使标题视图与用户一样滚动,就像app store的索引轮播标题一样?
或者我只是去了错误的地方,我应该用其他方法来接近。
解
-
当您将CollectionView1保存为TableViewHeader时,CollectionView1在到达顶部后将始终位于TableView的顶部。 如果希望Collection1和Collection2一起向上滚动,则需要将CollectionView1保留在单元格中,而不是标题中。
-
确保CollectionView2内容高度小于或等于TableViewCell的高度。 当我在App Store上查看时,他们总是使SubCollectionView内容高度等于TableViewCell的高度(如果他们使用TableView)。
结果
有关更多详细信息,您可以查看我的示例项目
https://github.com/trungducc/stackoverflow/tree/app-store-header
问题
1)你的tableview单元格Collectionview (比如collection2),集合2是可滚动的。 因此,当您向上滚动时,tableview将不会向上滚动
解
1)只是简单且工作的解决方案将是高度常数 ,你必须使用> =关系和0常量值和750优先级给予collection2高度常量!
现在的问题是如何使用它
您需要将IBOutlet
of Height常量保持为自定义tableview单元格,并且需要从那里管理collectionview高度。
这是一个例子
class FooTableViewCell: UITableViewCell { static let singleCellHeight = 88; @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var descriptionLabel: UILabel! @IBOutlet weak var iconsCollectionView: IconsCollectionView! @IBOutlet weak var const_Height_CollectionView: NSLayoutConstraint! var delegateCollection : TableViewDelegate? var bars:[Bar] = [] { didSet { self.iconsCollectionView.reloadData() iconsCollectionView.setNeedsLayout() self.layoutIfNeeded() let items:CGFloat = CGFloat(bars.count + 1) let value = (items / 3.0).rounded(.awayFromZero) const_Height_CollectionView.constant = CGFloat((iconsCollectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize.height * value) self.layoutIfNeeded() } } override func awakeFromNib() { iconsCollectionView.translatesAutoresizingMaskIntoConstraints = false iconsCollectionView.initFlowLayout(superviewWidth: self.frame.width) iconsCollectionView.setNeedsLayout() iconsCollectionView.dataSource = self iconsCollectionView.delegate = self const_Height_CollectionView.constant = iconsCollectionView.contentSize.height self.layoutIfNeeded() self.setNeedsLayout() } }
你可以检查我的答案使用UITableViewAutomaticDimension使用嵌入式UICollectionView制作UITableView非常相似且100%正常工作
另一种解决方案您还可以将UICollectionView
与包含另一个水平集合UICollectionView
的部分UICollectionView
使用,使用此解决方案,您无需为每个单元格管理内容大小
希望它有所帮助