在我的NSCollectionViewItem中,我设置了以下代码。
func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
let view = collectionView.makeSupplementaryView(ofKind: .sectionHeader, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "sectionHeader"), for: indexPath)
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.red.cgColor
return view
}
func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
return NSSize(width: self.frame.size.width, height: 60)
}但是,当我在viewForSupplementaryElementOfKind函数中放置一个print()时,它永远不会被执行。此外,也不会出现红色标题。
这就是我设置NSCollectionView的方法
public let collectionView: NSCollectionView = {
let v = NSCollectionView(frame: NSRect.zero)
v.wantsLayer = true
v.layer!.backgroundColor = NSColor.clear.cgColor
v.isSelectable = true
v.backgroundColors = [NSColor.clear]
v.allowsEmptySelection = false
v.collectionViewLayout = {
let l = NSCollectionViewFlowLayout()
l.minimumInteritemSpacing = 1
l.minimumLineSpacing = 1
l.scrollDirection = NSCollectionView.ScrollDirection.vertical
l.sectionInset = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
return l
}()
return v
}()我做错了什么?
发布于 2018-11-30 07:56:00
补充视图(页眉和页脚)不是集合视图项。您需要在NSCollectionViewDelgateFlowLayout对象上实现这两个方法,而不是在NSCollectionViewItem类上。
请参阅https://developer.apple.com/documentation/appkit/nscollectionviewdelegateflowlayout
您还可以为整个集合视图设置默认值:
l.headerReferenceSize = NSSize(width: 0, height: 50)
l.footerReferenceSize = NSSize(width: 0, height: 50)在使用NSCollectionViewFlowLayout时,页眉和页脚的默认大小为NSSize.zero,因此除非使用这些技术中的一种,否则不会调用生成补充视图的方法。
https://stackoverflow.com/questions/51775548
复制相似问题