1

我已经在情节提要中创建了 UICollectionView 并添加了页眉页脚视图,它工作正常。但我的问题是如何创建 UICollectionViewReusable 视图以编程方式添加为 SupplementaryView。我试过但没有调用委托。请注意我也设置了委托。下面的代码我有试过了

- (void)setUpCustomCollectionView
{

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, 320, 500) collectionViewLayout:layout];

    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"brandingHeaderView"];

    self.collectionView.bounces = NO;
    self.collectionView.tag = 10;
    self.collectionView.backgroundColor = [UIColor darkGrayColor];
    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];

    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;

    [self.baseScrollView addSubview:self.collectionView];
}

在委托中

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath
{
 if (kind == UICollectionElementKindSectionHeader) {
            UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"brandingHeaderView" forIndexPath:indexPath];

            UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 80)];
            view.backgroundColor = [UIColor redColor];

                 [headerView addSubview:view];

            return headerView;
        }
}

引导我。

4

5 回答 5

1

我刚刚遇到了类似的问题,没有调用以下代表...

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

然后我记得当我定义 UICollectionViewFlowLayout 的实例时,我已经按照以下代码分配了 itemSize 值......

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(106.f, 106.f);

还尝试在标题中添加以下行...

layout.headerReferenceSize = CGSizeMake(320.f, 30.f);
于 2014-09-20T00:45:31.043 回答
0

我猜这里有错误:

[UICollectionViewFlowLayout class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader

UICollectionViewFlowLayout不能是标题视图

编辑:

要使其工作,您需要 的子类UICollectionReusableView,不要忘记覆盖reuseIdentifier属性。还要检查文档:

UICollectionReusableView 类参考

于 2014-04-03T11:01:36.377 回答
0

要添加它,应创建一个名为 Header(Header.xib) 的自定义 nib 文件,并将 UILabel 从对象库中拖出并添加到 Header.xib 中。接下来创建一个自定义文件 UICollectionReusableView 的子类。例如 HeaderCollectionReusableView.swift 和 header.xib 可以看到它,并且标签的 IBOutlet 在这个自定义类中完成。

于 2015-12-21T09:12:53.213 回答
0
override init(collectionViewLayout layout: UICollectionViewLayout) {
    super.init(collectionViewLayout: layout)
    let nib = UINib(nibName: "Empty", bundle: nil)
    collectionView!.registerNib(nib, forCellWithReuseIdentifier: "cell")

    /* register the header nib's */
    let headerNib = UINib(nibName: "Header", bundle: nil)
    collectionView?.registerNib(headerNib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")


    /* register footers nib */
    let footerNib = UINib(nibName: "Footer", bundle: nil)
    collectionView?.registerNib(footerNib, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer")

    collectionView!.backgroundColor = UIColor.whiteColor()
}

convenience required init(coder aDecoder: NSCoder){
    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.minimumLineSpacing = 20
    flowLayout.minimumInteritemSpacing = 10
    flowLayout.itemSize = CGSize(width: 80, height: 120);

    flowLayout.scrollDirection = .Vertical
    flowLayout.sectionInset =
        UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)

    // set the header and footer views size properties
    flowLayout.headerReferenceSize = CGSize(width: 300, height: 50)
    flowLayout.footerReferenceSize = CGSize(width: 300, height: 50)
    self.init(collectionViewLayout: flowLayout)
}
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> HeaderCollectionReusableView{

    let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! HeaderCollectionReusableView
    view.headerLabel.text = "header section title"

    return view
}
于 2015-12-20T08:58:55.660 回答
-1

为了以编程方式将标题视图添加到 UICollectionView,您需要执行以下操作。

UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout alloc] init];
layout.headerReferenceSize = CGSizeMake(100.0f, 40.0f);

UICollectionView* _collectionView=[[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:UICollectionElementKindSectionHeader];

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath

if ([kind isEqualToString:UICollectionElementKindSectionHeader]){

UICollectionReusableView *reusableView = [collectionView      dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:UICollectionElementKindSectionHeader forIndexPath:indexPath];

        if (reusableView==nil) {
        reusableView=  [[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        label.text= @"Top stories";
        label.textColor = [UIColor blueColor];
        [reusableView addSubview:label];
        }
        return reusableView;
    }
    return nil;
}
于 2016-05-16T09:04:10.233 回答