在SWIFT4.2之前,我可以创建一个NSCollectionView头,如下所示:
func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
let view = collectionView.makeSupplementaryView(ofKind: .sectionHeader, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Header"), for: indexPath as IndexPath) as! Header
view.sectionTitle.stringValue = collectionSections[indexPath.section]
return view
}如果我没记错的话,.sectionHeader是来自NSCollectionView.SupplementaryElementKind的一个enum。但是医生们说NSCollectionView.SupplementaryElementKind是String。
这就留给我更新的SWIFT4.2代码,如下所示:
func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
let view = collectionView.makeSupplementaryView(ofKind: "?????", withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Header"), for: indexPath as IndexPath) as! Header
view.sectionTitle.stringValue = collectionSections[indexPath.section]
return view
}我不清楚我应该为ofKind参数(String)包括哪些内容。那串对应的是什么?在我的xib文件中,我没有看到任何与它相关联的东西。
发布于 2018-10-18 19:49:10
我想通了。您只需从它所在的kind委托方法中传入makeSupplementaryView参数。
let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Header"), for: indexPath as IndexPath) as! Headerhttps://stackoverflow.com/questions/52867739
复制相似问题