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

查看:28
本文介绍了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的kind,在这个例子中是一个header,由UICollectionViewFlowLayout类提供的常量字符串UICollectionElementKindSectionHeader 用于它.如果您注意到 viewForSupplementaryElementOfKind 上的参数,这个 kind 稍后会作为 kind: 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.

希望这会有所帮助!

参考点:

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天全站免登陆