简体   繁体   English

UITableView可以用UICollectionView滚动吗?

[英]Can UITableView scroll with UICollectionView inside it?

I have the structures below... 我有以下结构......

在此输入图像描述

I wrap two of collection views into tableview 我将两个集合视图包装到tableview中

One is in tableview header(Collection1), another is in tableview 1st row(Collection2). 一个在tableview标题(Collection1)中,另一个在tableview第一行(Collection2)中。

All the functions are good (Both collection view). 所有功能都很好(两个集合视图)。

just... 只是...

When I scroll up in Collection2, Collection1 will Not scroll up together, because I'm only scrolling the collectionViews not the tableview. 当我在Collection2中向上滚动时,Collection1将不会向上滚动,因为我只滚动collectionViews而不是tableview。

It only scroll together when I scroll in Collection1. 它只在我在Collection1中滚动时一起滚动。

Is it possible to make the header view scroll with user just like app store's index carousel header? 是否有可能使标题视图与用户一样滚动,就像app store的索引轮播标题一样?

Or I just went to the wrong place, I should use other ways to approach. 或者我只是去了错误的地方,我应该用其他方法来接近。

Solution

  • When you keep CollectionView1 as a TableViewHeader, CollectionView1 will always on the top of TableView after it reaches top. 当您将CollectionView1保存为TableViewHeader时,CollectionView1将在它到达顶部后始终位于TableView的顶部。 If you want Collection1 and Collection2 scroll up together, you need to keep CollectionView1 in a cell, not a header. 如果您希望Collection1和Collection2一起向上滚动,则需要将CollectionView1保留在单元格中,而不是标题中。

  • Make sure CollectionView2 content height smaller or equal to TableViewCell's height. 确保CollectionView2内容高度小于或等于TableViewCell的高度。 As I checked on App Store, they always make SubCollectionView content height equal to TableViewCell's height (If they use TableView). 当我在App Store上查看时,他们总是使SubCollectionView内容高度等于TableViewCell的高度(如果他们使用TableView)。

Result 结果

For more detail, you can take a look at my sample project 有关更多详细信息,您可以查看我的示例项目

https://github.com/trungducc/stackoverflow/tree/app-store-header https://github.com/trungducc/stackoverflow/tree/app-store-header

Problem 问题

1) Your tableview cell Collectionview (let's say collection2) , Collection 2 is scrollable. 1)你的tableview单元格Collectionview (比如collection2),集合2是可滚动的。 So when you scroll up tableview won't scroll up 因此,当您向上滚动时,tableview将不会向上滚动

Solution

1) Just simple and working solution would be height constant , You have to give height constant to the collection2 with >= relationship and 0 Constant value and 750 priority !! 1)只需简单且工作的解决方案就是高度常数 ,你必须使用> =关系和0常量值和750优先级给予collection2高度常量!

Now the question is how to use this 现在的问题是如何使用它

You need to take IBOutlet of Height constant to your custom tableview cell and need to manage the collectionview height from there. 您需要将IBOutlet of Height常量保持为自定义tableview单元格,并且需要从那里管理collectionview高度。

Here is example 这是一个例子

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()
    }
}

You can check my answer Making UITableView with embedded UICollectionView using UITableViewAutomaticDimension Very similar and 100% working 你可以检查我的答案使用UITableViewAutomaticDimension使用嵌入式UICollectionView制作UITableView非常相似且100%正常工作

Another solution You can also take UICollectionView with sections which contain another horizontal collectionview , with this solution you don't need to manage contentsize for every cell 另一种解决方案您还可以将UICollectionView与包含另一个水平集合UICollectionView的部分UICollectionView使用,使用此解决方案您无需为每个单元格管理内容大小

Hope it is helpful 希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM