Swift,集合视图:未显示节标头



我正试图在Swift、MacOS中编程一个NSCollectionView。它运行良好,只有一个例外。节标题不会出现。它们应该出现的地方只是一片空白。下面代码中的最后一个collectionView函数应该在每次绘制标头时调用,但从未被调用。相反,它抛出了这样的警告:

实例方法"collectionView(:viewForSupplementaryElementOfKind:at:("几乎与协议"NSCollectionViewDataSource"的可选要求"collectionView(:ViewForSupplemenaryElementOfKinD:at:(

有人知道我做错了什么吗?如何让程序调用补充视图collectionView函数?

最好!

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
}
}

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

此代码:

collectionView(_ collectionView:NSCollectionView,viewForSupplementaryElementOfKind kind:String,在indexPath:indexPath(->NSView

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

最新更新