嵌入在UITableViewCell中的动态自定义布局UICollectionView

时间:2018-04-19 16:12:57

标签: ios swift uitableview uicollectionview uikit

我的UITableViewController有1个部分,头部有UISegmentedControl。

If user presses first segment we should display UITableView cells

If user presses second segment we should display custom vertical scrolling collection grid with collection items below that header

可能的解决方案

  1. 将UITableViewController更改为UICollectionViewController,将UITableViewCells更改为UICollectionViewCells。这应该可以,但我试图避免这个解决方案

  2. UICollectionView嵌入在UITableViewCell中。将UICollectionView滚动设置为false,调整UITableViewCell的高度以适应整个UICollectionView的contentSize.height。在容器(UITableViewCell)的systemLayoutSizeFitting(...)方法中,我们在容器和包含的UICollectionView上调用layoutIfNeeded并返回UICollectionView的contentSize。

    问题:

    (重新)计算整个UICollectionView contentSize需要大量的工作,如果我们有数百个项目(每个项目的cellForItemAtIndexPath)。想以某种方式避免这种情况。也许我们可以在滚动内部的UICollectionView期间增加容器(UITableViewCell)高度。

  3. 与2相同,但我们有固定大小的容器UITableViewCell(与没有标题的视图相同的高度),里面有UICollectionView。当我们将UITableView滚动到最底部时,UICollectionView滚动应该继续滚动。

    问题:

    我不明白如何使用scrollingEnabled = true来处理两个UIScrollView实例。我认为这可以通过平移手势识别器代表来解决。如果是这样,我需要帮助。

1 个答案:

答案 0 :(得分:0)

我个人会选择1号并完全避免使用2号。对于3号,您可以在点击第2段时设置tableView.scrollingEnabled = false。点击第一个段时将其设置为true。这样,当它false时,UICollectionView中的UITableViewCell仍可以滚动。

This tutorial可能有助于将UICollectionView放入UITableViewCell

相关问题