简体   繁体   English

如何在自定义UICollectionViewCell和CollectionViewLayout中使用rbcollectionviewinfofolderlayout

[英]How to use rbcollectionviewinfofolderlayout with custom UICollectionViewCell & CollectionViewLayout

What I want to do is display a UICollectionView inside a UICollectionViewCell when this cell is selected. 我想要做的是在选择此单元格时在UICollectionViewCell内显示UICollectionView。 I want to use rbcollectionviewinfofolderlayout to fold out my collection view and show a new collection view inside of this. 我想使用rbcollectionviewinfofolderlayout来折叠我的集合视图并在其中显示一个新的集合视图。 However I'm not sure how I can implement this into my existing code. 但是,我不确定如何将其实现到现有代码中。 I have three collection views inside one view. 我在一个视图中有三个集合视图。 I hide and unhide my views accordingly to what selection the user makes. 我根据用户的选择隐藏和取消隐藏我的观点。 I use a custom cell xib for the cells inside my collection views and I have a custom collectionviewflowlayout that makes sure there are always 3 cells showing in the width of the device. 我为集合视图中的单元格使用自定义单元格xib,并且我有一个自定义collectionviewflowlayout,确保在设备的宽度中始终显示3个单元格。

This is what my view controller looks like. 这就是我的视图控制器的样子。

in my viewDidLoad I have to set the RBCollectionViewInfoFolderLayout to my collectionview. 在我的viewDidLoad中,我必须将RBCollectionViewInfoFolderLayout设置为我的collectionview。 As you can see the layout variable holds my CustomCollectionViewFlow which I set as the collection view layout before I implemented the RBCollectionviewinfofolderlayout. 正如您所看到的,布局变量包含我的CustomCollectionViewFlow ,我在实现RBCollectionviewinfofolderlayout之前将其设置为集合视图布局。

    override func viewDidLoad() {
    super.viewDidLoad()

    musicLib.loadLibrary()

    PlaylistCollectionView.indicatorStyle = UIScrollViewIndicatorStyle.White
    AlbumCollectionView.indicatorStyle = UIScrollViewIndicatorStyle.White
    ArtistCollectionView.indicatorStyle = UIScrollViewIndicatorStyle.White

    layout = CustomCollectionViewFlow()

    cview = ArtistCollectionView

    let lay: RBCollectionViewInfoFolderLayout = ArtistCollectionView.collectionViewLayout as! RBCollectionViewInfoFolderLayout
    lay.cellSize = CGSizeMake(80, 80)
    lay.interItemSpacingY = 10
    lay.interItemSpacingX = 0

    let nib = UINib(nibName: "CollectionViewCell", bundle: nil)

    cview.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: RBCollectionViewInfoFolderHeaderKind, withReuseIdentifier: "header")
    cview.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: RBCollectionViewInfoFolderFooterKind, withReuseIdentifier: "footer")
    cview.registerClass(collectionViewFolder.self, forSupplementaryViewOfKind: RBCollectionViewInfoFolderFolderKind, withReuseIdentifier: "folder")
    cview.registerClass(RBCollectionViewInfoFolderDimple.self, forSupplementaryViewOfKind: RBCollectionViewInfoFolderDimpleKind, withReuseIdentifier: "dimple")


    ArtistCollectionView.collectionViewLayout = lay
    ArtistCollectionView.registerNib(nib, forCellWithReuseIdentifier: "item")
    ArtistCollectionView.dataSource = self
    ArtistCollectionView.delegate = self

    PlaylistCollectionView.collectionViewLayout = layout
    PlaylistCollectionView.registerNib(nib, forCellWithReuseIdentifier: "item")
    PlaylistCollectionView.dataSource = self

    AlbumCollectionView.collectionViewLayout = layout
    AlbumCollectionView.registerNib(nib, forCellWithReuseIdentifier: "item")
    AlbumCollectionView.dataSource = self
}

My CustomCollectionViewFlow looks like this 我的CustomCollectionViewFlow看起来像这样

class CustomCollectionViewFlow: UICollectionViewFlowLayout{
override init(){
    super.init()
    setupLayout()
}

required init?(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)
    setupLayout()
}

override var itemSize: CGSize {
    set {

    }
    get {
        let numberOfColumns: CGFloat = 3

        let itemWidth = (CGRectGetWidth(self.collectionView!.frame) - (numberOfColumns - 1)) / numberOfColumns
        return CGSizeMake(itemWidth, itemWidth)
    }
}

func setupLayout() {
    minimumInteritemSpacing = 0
    minimumLineSpacing = 0
    scrollDirection = .Vertical
}

}

I will hold of on putting all my coding here as it will be become to big of a post and the other methodes are kind of irrelevant at this point. 我将把所有编码放在这里,因为它将成为一个大的帖子,其他方法在这一点上是无关紧要的。 However what I did do is put the example application that I made for this on git here for anyone who wants to check it out. 然而,我所做的是将我为此制作的示例应用程序放在git上以供任何想要查看它的人使用。

This image shows what the state of my collectionview was before I implemented the rbcollectionview. 在我实现rbcollectionview之前,此图显示了我的collectionview的状态。 The second image shows what I'm trying to achieve 第二张图显示了我想要实现的目标

在此输入图像描述

This is how the view should look when an item is being tapped 这是查看项目时视图的外观

在此输入图像描述

EDIT 编辑

I have been able to get it working kind of. 我已经能够让它工作了。 I was able to show the layout like I desired. 我能够像我想要的那样展示布局。 Just like I had it before I implemented the rbcollectionviewinfofolderlayout. 就像我在实现rbcollectionviewinfofolderlayout之前一样。 However it seems that when the folder is bigger then the screen size it won't actually fold out. 然而,似乎当文件夹较大时,屏幕尺寸实际上不会折叠。 It will fold out for a second and collapse again. 它将折叠一秒钟然后再次崩溃。 It might be caused by the layout i've implemented. 它可能是由我实施的布局引起的。 Below is the code that is responsible for this. 以下是负责此操作的代码。

the class that is responsible for my layout 负责我的布局的类

class RBCollectionLayout: RBCollectionViewInfoFolderLayout
{
    var view: UIView!
    init(view: UIView){
        super.init()
        self.view = view
        setupLayout()
    }

override init(){
    super.init()
    setupLayout()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupLayout()
}

func setupLayout(){
    let numberofItems: CGFloat = 3

    let itemWidth = (CGRectGetWidth(view.frame)) / numberofItems
    cellSize = CGSizeMake(itemWidth, itemWidth)

    interItemSpacingX = 0
    interItemSpacingY = 0
}
}

The method that will calculate the desired with when the screen is changed from portrait to landscape 当屏幕从纵向更改为横向时,将计算所需的方法

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    if(view_artist.hidden == false){
        guard let ArtistFlowLayout = ArtistCollectionView.collectionViewLayout as? RBCollectionViewInfoFolderLayout else {
            return
        }
        lay = RBCollectionLayout(view: self.view)
        ArtistCollectionView.collectionViewLayout = lay
        ArtistFlowLayout.invalidateLayout()
    }
}

this is how the layout is being set in my viewdidload 这是我在viewdidload中设置布局的方式

lay = RBCollectionLayout(view: self.view)
ArtistCollectionView.collectionViewLayout = lay

again all my code is available on my git here 我的git 在这里再次提供所有代码

Well, I'm probably the only person who can answer your question, since I wrote this control and I doubt anyone else is actually using it. 好吧,我可能是唯一可以回答你问题的人,因为我写了这个控件,我怀疑其他人是否真的在使用它。

You seem to be missing a key component which is the delegate method collectionView:viewForSupplementaryElementOfKind:atIndexPath: where you tell the CollectionView what views to use for the various elements. 您似乎缺少一个关键组件,它是委托方法collectionView:viewForSupplementaryElementOfKind:atIndexPath:您告诉CollectionView哪些视图用于各种元素。 If you go to the GitHub Repo and look at the example you will see that in that method I return 4 different views depending on the viewKind that was asked for. 如果您转到GitHub Repo并查看示例,您将看到在该方法中,我将返回4个不同的视图,具体取决于所要求的viewKind。 In your code I believe what you want to do is return your custom flow layout based collection view for the RBCollectionViewInfoFolderFolderKind . 在您的代码中,我相信您要做的是返回RBCollectionViewInfoFolderFolderKind基于自定义流布局的集合视图。 This would place your 3 cell flow layout view into the expanded folder of the selected cell. 这会将您的3单元格流布局视图放入所选单元格的展开文件夹中。

I cloned your repo but it doesn't appear to be up to date with the code you are showing here. 我克隆了您的回购,但它似乎与您在此处显示的代码保持同步。

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

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