Q:

Hide collectionView content when collectionView is collapsed

I have a collectionView with expandable and collapsible sections. When I collapsed the sections, the content inside of the table View is still present. I was wondering if anyone knew how to change this? I have a UicollectionViewCell in which I create the views programmatically. However, I was thinking that my issues arrises because I'm not creating the views inside of the cellForItemAt. I will leave my code below please let me know what you think. 
// this is how I create the sectionHeaders and register the collectionViewCell. 
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    var reusableview = UICollectionReusableView()
    if (kind == UICollectionView.elementKindSectionHeader) {
        let section = indexPath.section
        switch (section) {
        case 0:
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as? userProfileHeader
                        if let user = self.user {
                                headerView?.myUser = user
                            } else if let userToLoad = self.userToLoad {
                                headerView?.myUser  = userToLoad
                            }
            reusableview = headerView!

        case 1:
            let sectionViews = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: skillsSection, for: indexPath) as? skillsprefSection
            sectionViews?.downArrowBtn.addTarget(self, action: #selector(showCells), for: .touchUpInside)
            sectionViews?.headerLabel.text = "Skills & Preferences"

            if expandedRow == false {
                sectionViews?.contentView.isHidden = true
            }

            reusableview = sectionViews!

        case 2:
            let bioSectionThing = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: bioSectionHeader, for: indexPath) as? bioSection
                bioSectionThing?.downArrowBtn.addTarget(self, action: #selector(showCells), for: .touchUpInside)
                bioSectionThing?.headerLabel.text = "Bio"
                reusableview = bioSectionThing!

        case 3:
            let reviewSectionThing = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: reviewSectionHeader, for: indexPath) as? reviewSection
                reviewSectionThing?.downArrowBtn.addTarget(self, action: #selector(showCells), for: .touchUpInside)
                reviewSectionThing?.headerLabel.text = "Reviews"
                reusableview = reviewSectionThing!

         case 4:
                  let recentSectionThing = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: recentSectionHeader, for: indexPath) as? recentSection
                    recentSectionThing?.downArrowBtn.addTarget(self, action: #selector(showCells), for: .touchUpInside)
                      recentSectionThing?.headerLabel.text = "Recent"
                      reusableview = recentSectionThing!
                default:
            return reusableview

        }
    }
    return reusableview

}

// this is how I expand/contract the sections 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

    if(section==0) {
        return .init(width: view.frame.width, height: 340)

    } else if (section==1) {
        if expandedRow == true {
               return .init(width: view.frame.width, height: 450)
           } else {
               return .init(width: view.frame.width, height: 133)
           }

    } else if (section==2) {
         if expandedRow == true {
        return .init(width: view.frame.width, height: 400)
        } else {
        return .init(width: view.frame.width, height: 133)
        }

    } else if (section==3) {
    if expandedRow == true {
    return .init(width: view.frame.width, height: 400)
    } else {
    return .init(width: view.frame.width, height: 133)
    }

    } else if (section==4) {
        if expandedRow == true {
        return .init(width: view.frame.width, height: 400)
        } else {
        return .init(width: view.frame.width, height: 133)
    }

    } else {
        return .init(width: view.frame.width, height: 100)
    }

}

this is how the image looks when collapsed. 

and this is how the section looks when expanded.
Thanks for any and all help. My goal is for when the section is collapsed, to hide all of the content associated with that section. and show it when the section is expanded.

A:

Your collection view cell out of bound just set as follow
cell.contentView.clipsToBounds = true
cell.clipsToBounds = true

