UICollectionView标题视图以编程方式创建并添加?

时间:2014-04-03 10:43:12

标签: iphone uicollectionview uicollectionreusableview

我在故事板中创建了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 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

我猜错了:

[UICollectionViewFlowLayout class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader

UICollectionViewFlowLayout无法成为标题视图

编辑:

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

UICollectionReusableView Class Reference

答案 2 :(得分: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
}

答案 3 :(得分:-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;
}