我正在使用viewForSupplementaryElementOfKind
在我的收藏视图中生成标题。
header ( SectionHeader
) 是 Storyboard 中的一个 Section Header 附件,仅包含 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 设置。
问题是当这个视图控制器加载时,标题显示为“选择”
当我将标题从屏幕上滚动出来,然后又回到屏幕上时,标题会正确显示:“选择 Burton Bindings”
我sectionTitle
在 viewWillAppear 中进行了测试,并打印了正确的数据。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("viewWillAppear ----- \(sectionTitle)")
}
(印刷viewWillAppear ----- Burton Bindings
)
我想我的问题是由于 viewForSupplementaryElementOfKind 的生命周期,以及何时被调用?
如何在 VC 加载时显示部分标题,而不是在屏幕上滚动标题以使其显示?