如何在UICollectionView的Section Header中显示图像和标签?

时间:2017-01-24 00:48:11

标签: ios swift uicollectionview uicollectionviewlayout uicollectionreusableview

在我的swift应用中,我使用UICollectionViewController,其中我一个接一个地展示照片:

enter image description here

我还在集合视图的顶部添加了一个标题,我通过检查此选项通过故事板完成了它:

enter image description here

所以现在我在故事板上看到了这个标题,我甚至可以在那里贴上一些标签:

enter image description here

但我怎么能显示任何东西?我尝试将此标签中的IBOutlet附加到该视图的主要类,但这会让我误以为来自Collection reusable view的出口无法直接附加到UICollectionViewcontroller。在这种情况下我该怎么办?

1 个答案:

答案 0 :(得分:3)

要实现此目的,您需要子类化UICollectionViewController并将此类指定为页眉和页脚。然后在该类中,您可以创建在故事板中分配的视图的Outlet连接。

UICollectionReusableView的子类:

enter image description here

将您的子类分配给UICollectionViewController标头:

Assign subclass to the UICollectionViewController header

将元素拖到故事板中的标题,然后创建IBOutlet连接。

Create the IBOutlet connection