带有两个标头的UICollectionView

can*_*boy 19 iphone objective-c ios uicollectionview uicollectionviewlayout

在UICollectionView中有两个标头?

我有一个UICollectionView使用流布局,它也有一个页眉和页脚:

---------   
| head  |
---------
| A | B |
---------
| C | D |
---------
| foot  |
---------
Run Code Online (Sandbox Code Playgroud)

偶尔,我想要两个标题,如下所示:

---------   
| head1 |
---------   
| head2 |
---------
| A | B |
---------
| C | D |
---------
| foot  |
---------
Run Code Online (Sandbox Code Playgroud)

我坚持如何实现这一目标.流动布局似乎只允许一个头和一个脚.如何添加第二个标题?


编辑:我还实现了粘贴标题 - http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using - 但我只希望第一个标题是粘性的.这就是为什么我不能只在一个标题中包含所有内容.

War*_*olf 35

你只需要使用一个简单的技巧.显示所有部分的页眉和页脚.

在哪个部分你不想显示页脚只是传递其大小为零: -

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if(section==0)
    {
        return CGSizeZero;
    }

    return CGSizeMake(320, 50);
}
Run Code Online (Sandbox Code Playgroud)

在这里,我使用了两个部分

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
Run Code Online (Sandbox Code Playgroud)

并且只传递了最后一个部分中的行数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==1) {
        return 20;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我的输出---

在此输入图像描述

红色视图是标题,绿色是页脚.

在这里,您可以获得整个实施文件


Sac*_*hin 5

这些内容可以帮助您实现您想要的目标

创建类CollectionHeaderView并使其派生UICollectionReusableView并制作容器,然后在制作2 uiview后将其放入此容器中

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader) {
        CollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        headerView.firstContainer.titleLabel.text = @"MY Header View 1";//Here you can set title 

        headerView.secondContainer.titleLabel.text = @"MY Header View 2";//Here you can set title  
        UIImage *headerImage = [UIImage imageNamed:@"header_banner.png"];
        headerView.firstContainer.backgroundImage.image = headerImage;
       headerView.secondContainer.backgroundImage.image = headerImage;

        reusableview = headerView;
    }

    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];

        reusableview = footerview;
    }

    return reusableview;
}
Run Code Online (Sandbox Code Playgroud)