【发布时间】:2020-04-19 22:40:46
【问题描述】:
我正在使用viewForSupplementaryElementOfKind 在我的收藏视图中生成标题。
标题 (SectionHeader) 是 Storyboard 中的部分标题附件,仅包含 1 个插座。
class SectionHeader: UICollectionReusableView {
@IBOutlet weak var sectionHeaderlabel: UILabel!
}
这是我对viewForSupplementaryElementOfKind的实现
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind:
String, at indexPath: IndexPath) -> UICollectionReusableView {
print("SECTION TITLE (brand of bindings) --------> \(sectionTitle)")
if let sectionHeader = allCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "bindingsID", for: indexPath) as? SectionHeader{
sectionHeader.sectionHeaderlabel.text = "Select \(sectionTitle)"
return sectionHeader
}
return UICollectionReusableView()
}
sectionTitle 通过 segue 设置。
问题是当这个 View Controller 加载时,标题显示为“Select”
当我将标题从屏幕上滚动出来,然后又回到屏幕上时,标题会正确显示:"Select Burton Bindings"
我在 viewWillAppear 中测试了sectionTitle,并打印了正确的数据。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("viewWillAppear ----- \(sectionTitle)")
}
(打印viewWillAppear ----- Burton Bindings)
我想我的问题是由于 viewForSupplementaryElementOfKind 的生命周期,以及它何时被调用?
如何在 VC 加载时显示部分标题,而不是在屏幕上滚动标题才能显示?
【问题讨论】:
标签: swift uicollectionview collectionview uicollectionreusableview sectionheader