在Swift 4.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
来自enum
的{{1}}。但是the docs说NSCollectionView.SupplementaryElementKind
是NSCollectionView.SupplementaryElementKind
。
这给我留下了更新的 Swift 4.2 代码,如下所示:
String
我不清楚我应该为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
)添加什么内容。该字符串对应什么?我的xib文件中没有其他与其关联的文件。
答案 0 :(得分:1)
我知道了。您只需从其内部的kind
委托方法中传入makeSupplementaryView
参数即可。
let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Header"), for: indexPath as IndexPath) as! Header