UICollectionView多个部分和标题 [英] UICollectionView Multiple Sections and Headers

查看:86
本文介绍了UICollectionView多个部分和标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力尝试在我的集合视图中使用每个部分的标题执行多个部分。我不知道Obj-C,我已经找到了很多教程,但还是没能弄清楚如何将它转换成Swift。

I am struggling trying to do multiple sections in my collection view with a header for each section. I don't know Obj-C and I've found a good amount of tutorials for it, but haven't been able to figure out how to convert it into Swift.

我的所有数据都是静态的,所以我需要的是某种可用于创建多个部分的数组或字典。我已经有一个包含1个部分的集合视图,所以如果你有任何见解或代码可以帮助多个部分。

All my data is static so all I need is some sort of array or dictionary that I can use to create the multiple sections. I already have a collection view with 1 section working, so if you have any insight or code for multiple sections that'd be helpful.

我知道如何使用

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int   {
    return sectionData.count
}

我觉得我最重要的是需要帮助才能实现这个功能

I think the main thing I need help with is implementing this func

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { }

并设置数据!

UICollectionView和UITableView几乎完全相同,所以如果你知道如何在Swift的UITableView中做多个部分,你的帮助也很感激

UICollectionView and UITableView are almost exactly the same, so if you know how to do multiple sections in a UITableView in Swift, your help is also appreciated

推荐答案

cellForItemAtIndexPath 函数处理用单元格填充每个部分,它不处理部分或补充视图,因此,在创建节标题时,您不需要帮助。

The cellForItemAtIndexPath function handles populating each section with cells, it does not handle sections or supplementaryViews, and therefore is not the main thing you need help with when it comes to creating section headers.

您需要实现的方法是 viewForSupplementaryElementOfKind 。它的签名是:

the method you need to implement is viewForSupplementaryElementOfKind. Its signature is:

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {}

假设你的collectionView在1节中正常工作(你已经正确填写了正文你的cellForItemAtIndexPath和你的sectionData数组正确地反映了你想要显示的部分的数量,你应该能够使用以下指针实现节标题:

Assuming that your collectionView is working correctly for 1 section (you have properly filled out the body of your cellForItemAtIndexPath and your sectionData array properly reflects the number of sections you want to display), you should be able to implement section headers using the following pointers:

与细胞一起, UICollectionView 还支持补充视图对象,通常用于页眉或页脚。这些补充视图与 UICollectionViewCell 对象的行为非常相似。与 cellForItemAtIndexPath 处理单元格的方式相同, viewForSupplementaryElementOfKind 函数处理补充视图。

Along with cells, UICollectionView also supports "supplementary" view objects, typically used for headers or footers. These Supplementary Views act very similarly to UICollectionViewCell objects. In the same way that cellForItemAtIndexPath handles cells, The viewForSupplementaryElementOfKind function handles supplementary views.

要实现它,您需要先准备ViewController才能实现。首先编辑您的布局对象以反映每个标头将遵守的适当标头大小:

To implement it, you will need to first prepare your ViewController to do so. First edit your layout object to reflect an appropriate header size, that each header will adhere to:

 let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
 layout.headerReferenceSize = CGSize(width: self.view.frame.size.width, height: 30)

注意:我使用的是UICollectionViewFlowLayout

NOTE: I am using a UICollectionViewFlowLayout

接下来,如果您还没有这样做,请创建一个SectionHeader类来定义每个节头对象,所以你可以用你的collectionView对象注册那个类,如下所示:

Next, if you haven't already done so, create a SectionHeader class that defines each section header object, so you can then register that class with your collectionView object like so:

  collectionView!.registerClass(SectionHeaderView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeaderView");

这里,传入的第一个和第三个参数与UICollectionViewCell类注册相同,第一个参数在此方法中是对您创建的节头类的引用。第三个是补充视图的重用标识符。

Here, the first and third argument passed in are the same as a UICollectionViewCell Class registration, the first argument in this method is the reference to the section header class you created. The third is the reuse identifier for the Supplementary View.

第二个参数特定于Supplementary Views,它设置SupplementaryView的,在这种情况下是一个标题,常量字符串由UICollectionViewFlowLayout类提供 UICollectionElementKindSectionHeader 用于它。如果您注意到 viewForSupplementaryElementOfKind 上的参数,则此种类稍后将作为种类传递:String 参数。

The second argument is specific to Supplementary Views, this sets the kind of the SupplementaryView, which in this case is a header, the constant string provided by the UICollectionViewFlowLayout class UICollectionElementKindSectionHeader is used for it. If you noticed the parameters on the viewForSupplementaryElementOfKind, this kind is later passed in as the kind: String parameter.

以与对于cellForItemAtIndexPath函数相同的方式填写 viewForSupplementaryElementOfKind 的正文 - 使用 dequeueReusableSupplementaryViewOfKind 创建SectionHeader对象的方法,然后根据需要设置任何属性(标签,颜色等),最后返回标题对象。

Fill in the body of your viewForSupplementaryElementOfKind the same way you would for a cellForItemAtIndexPath function-- Using the dequeueReusableSupplementaryViewOfKind method to create a SectionHeader object, then set any attributes as necessary (labels, colors, etc.) and finally return the header object.

希望这会有所帮助!!

Hope this helps!!

参考点:

https: //developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewDataSource_protocol/index.html#//apple_ref/occ/intfm/UICollectionViewDataSource/

https:/ /developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewFlowLayout_class/index.html#//apple_ref/c/data/UICollectionElementKindSectionHeade

这篇关于UICollectionView多个部分和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆