【发布时间】: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