【发布时间】:2020-01-28 05:19:12
【问题描述】:
我有一个带有可展开和可折叠部分的 collectionView。当我折叠这些部分时,表格视图中的内容仍然存在。我想知道是否有人知道如何改变这个?我有一个 UicollectionViewCell,我在其中以编程方式创建视图。但是,我认为我的问题出现了,因为我没有在 cellForItemAt 内创建视图。我将在下面留下我的代码,请让我知道您的想法。
// 这就是我创建sectionHeaders 和注册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
}
//这就是我扩展/收缩部分的方式
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)
}
}
这就是该部分在展开时的样子。
感谢您的任何帮助。我的目标是在该部分折叠时隐藏与该部分关联的所有内容。并在部分展开时显示。
【问题讨论】:
-
cell.contentView.clipsToBounds = true & cell.clipsToBounds = true
-
@HarshalValanda 希望您将此作为答案发布,这有助于我完成所需的工作。
-
????????好的发布@zach
标签: ios xcode uicollectionview uicollectionviewcell