【问题标题】:Swift, Collection View: Section Headers not shownSwift,集合视图:未显示部分标题
【发布时间】:2019-05-10 22:02:08
【问题描述】:

我正在尝试在 Swift、MacOS 中编写 NSCollectionView。它运行良好,只有一个例外。部分标题不会出现。他们应该出现的地方只是空白。下面代码中的最后一个 collectionView 函数,应该在每次绘制标题时调用,永远不会被调用。相反,它会抛出这个警告:

实例方法 'collectionView(:viewForSupplementaryElementOfKind:at:)' 几乎匹配协议 'NSCollectionViewDataSource' 的可选要求 'collectionView(:viewForSupplementaryElementOfKind:at:)'

有人知道我做错了什么吗?如何让程序调用supplementary-view-collectionView-function?

最好的!

extension FacetWindow: NSCollectionViewDataSource{
        func numberOfSections(in collectionView: NSCollectionView) -> Int {
        return facets.count // how many sections?
    }

    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {

        return facets.facets[section].facetContent.count //how many items in section?
    }

    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
        let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "FacetsWindowCollectionViewItem"), for: indexPath)
        guard let collectionViewItem = item as? FacetsWindowCollectionViewItem else {return item}
        collectionViewItem.textField?.stringValue = facets[indexPath[0], indexPath[1]]
        print("facet" , facets[indexPath[0], indexPath[1]])
        return item
    }

   func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> NSView {
        //this function never gets called!!!
        let view = collectionView.makeSupplementaryView(ofKind: NSCollectionView.SupplementaryElementKind.sectionHeader, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "FacetsWindowCollectionViewHeader"), for: indexPath) as! FacetsWindowCollectionViewHeader
        view.FacetsWindowCollectionViewHeaderLabel.stringValue = facets[indexPath[0]]
        return view
        }

}

【问题讨论】:

  • 很难看出你做错了什么,因为你没有解释你的变量。什么是 FacetsWindowCollectionViewHeader,它是如何创建的?什么是刻面?没有任何解释。
  • 你查看collectionView(_:viewForSupplementaryElementOfKind:at:)的文档了吗?您的声明是否相同?

标签: swift macos collections header nscollectionview


【解决方案1】:

谢谢大家,我找到答案了!

这段代码:

collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> NSView

从未被调用,因为系统不再知道它。这是我从代码示例中复制粘贴的旧代码。手动重新输入,XCode 提出了正确的建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多