gpt4 book ai didi

swift - 带页眉和页脚的 NSCollectionView

转载 作者:搜寻专家 更新时间:2023-10-31 08:32:44 25 4
gpt4 key购买 nike

我需要这样的东西:

enter image description here

但是我的代码是这样做的:

enter image description here

func collectionView(collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> NSView {
let a = myView()
if kind == NSCollectionElementKindSectionHeader {
a.frame = NSMakeRect(0, 0, 1000, 10)
}
else if kind == NSCollectionElementKindSectionFooter {
a.frame = NSMakeRect(0, 0, 1000, 12)
}
return a
}

func numberOfSectionsInCollectionView(collectionView: NSCollectionView) -> Int {
return 3
}

override func numberOfItemsInSection(section: Int) -> Int {
if section == 0 {
return 5
}
else if section == 1 {
return 5
}
return 10
}

两个页眉和页脚位于 x=0,y=0 的位置,那么我的自定义 View (页眉和页脚)如何计算 y 位置?

最佳答案

根据 Apple 的示例项目 CocoaSlideCollection,部分页眉和页脚可用于 NSCollectionViewFlowLayout,查看 AAPLBrowserWindowControllerAAPLWrappedLayout了解更多详情。

转换成Swift和英文:P,简述如下:

  1. 实现协议(protocol) NSCollectionViewDelegateFlowLayout
  2. 实现 referenceSizeForHeaderInSectionreferenceSizeForFooterInSection 两个方法
  3. viewForSupplementaryElementOfKind 将在第 2 步之后调用

对于第 3 步,我使用以下代码

func collectionView(collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> NSView {
var nibName: String?
if kind == NSCollectionElementKindSectionHeader {
nibName = "Header"
} else if kind == NSCollectionElementKindSectionFooter {
nibName = "Footer"
}
let view = collectionView.makeSupplementaryViewOfKind(kind, withIdentifier: nibName!, forIndexPath: indexPath)
return view
}

这里使用的标识符nibName,链接到我创建的两个nib文件来设置NSView,分别是Header.xibFooter.xib

这是我得到的结果collectionView。

NSCollectionView with Header and Footer

希望对您有所帮助,我正在为此创建一个视频教程。帮帮忙。

编辑 1:

Sample Code现在可用

关于swift - 带页眉和页脚的 NSCollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com