2016-01-13 190 views
2

我需要的是这样的:NSCollectionView与页眉和页脚

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,所以如何计算我的自定义视图中的y位置(Header和页脚)?

+0

你所需的水平滚动或固定宽度? –

+0

@ArunAmmannaya固定宽度 – Geek20

+0

然后使用UITableview可以轻松实现。 UITableview非常适合这种需求。 –

回答

2

根据苹果的样本项目CocoaSlideCollection,部分页眉和页脚可供NSCollectionViewFlowLayout,检查出AAPLBrowserWindowControllerAAPLWrappedLayout的更多细节。

要转换成夫特和英文:P,简要解释是如下:

  1. 实现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,链接到两个笔尖文件我创建设置的NSView,即Header.xibFooter.xib

这是结果的CollectionView我得到。

NSCollectionView with Header and Footer

希望它能帮助,我创建了这个视频教程。帮助它。

编辑1:

Sample Code现已

+1

**它的工作原理!!! **感谢您转换成Swift和English:P – Geek20

+0

感谢您的支持。 –

0

我想你应该检查两件事情

首先是点击您的集合视图属性检查

你可以看到检查两个复选框,这样

配件*节头,页脚节

二是覆盖关键字

我想你应该把你的func关键字前面的override关键字!

+0

确实;为什么要重写?他只是在协议中实现一项功能。编译器应该抱怨它不是重写方法。 –

+0

您确定关于_section标题,章节Footer_?因为我没有看到他们。 _Xcode 7.2_ – Geek20

+0

@NicolasMiari哦,我不知道它,如果它是协议,这不是问题! –