【问题标题】:What to Use in NSCollectionView's SupplementaryElementKind String Value in Swift 4.2在 Swift 4.2 中 NSCollectionView 的 SupplementaryElementKind 字符串值中使用什么
【发布时间】:2018-10-18 05:48:57
【问题描述】:

在 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,来自NSCollectionView.SupplementaryElementKind。但是the docs 说NSCollectionView.SupplementaryElementKind 是String。

这给我留下了更新的 Swift 4.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 文件中看不到任何其他可以与之关联的内容。

【问题讨论】:

    标签: cocoa nscollectionview swift4.2


    【解决方案1】:

    我想通了。您只需从它所在的 makeSupplementaryView 委托方法传入 kind 参数。

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

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      相关资源
      最近更新 更多