在Swift 4.2中NSCollectionView的SupplementaryElementKind字符串值中使用什么

时间:2018-10-18 05:48:58

标签: cocoa nscollectionview swift4.2

在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 docsNSCollectionView.SupplementaryElementKindNSCollectionView.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文件中没有其他与其关联的文件。

1 个答案:

答案 0 :(得分:1)

我知道了。您只需从其内部的kind委托方法中传入makeSupplementaryView参数即可。

let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Header"), for: indexPath as IndexPath) as! Header