0

我正在尝试以collectionView编程方式将 imageView 添加到我的顶部(类似于 a tableViewHeaderView),但到目前为止,我正在尝试的viewDidLoad内容并不完全有效。

UIImageView *headerImageView = [[UIImageView alloc] init];

if ([isHeaderVisible intValue]== YES) {

    NSLog(@"Header View was found.");

    [headerImageView setImage:[UIImage imageNamed:headerImage]];
    [headerImageView setUserInteractionEnabled:YES];
    [headerImageView setFrame:CGRectMake(0, 0, 320, 160)];
    [headerImageView setContentMode:UIViewContentModeScaleAspectFit];

}

else {

    NSLog(@"No Header view found.");
    [headerImageView setImage:nil];
    [headerImageView setFrame:CGRectMake(0, 0, 0, 0)];
}

是否找到标题视图的逻辑正在运行,但我无法UIImageView正常工作。任何帮助将不胜感激!

PS 这不是用于 Section Header Title View,它类似于headerView在 Apple 的 App Store 中找到的。

更新:

我还在我的viewController. 所以基本上我想使用节标题和视图标题。我如何能够使用以下内容创建节标题和视图标题:

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

    if (kind == UICollectionElementKindSectionHeader) {
        DetailCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        headerView.sectionTitle.text = collectionSectionTitle;
        headerView.backgroundImage.image = [UIImage imageNamed:@"WFSectionHeader.png"];

        reusableview = headerView;
    }

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

        reusableview = footerview;
    }

    return reusableview;
}
4

2 回答 2

0

使用 uicollectionview 的委托方法之一

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

    if(kind == UICollectionElementKindSectionHeader)
    {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        //_scroll.frame = view.frame;
        [view addSubview:_scroll];
         _scroll.center = CGPointMake(self.view.frame.size.width / 2, _scroll.center.y);
        //_scroll.contentSize = CGSizeMake(view.frame.size.width * (MAXBANNER - 1),_scroll.frame.size.height);
    _scroll.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    }
    return view;
}
于 2013-06-10T00:23:43.977 回答
0

工作代码。尝试将 subView 添加到 reusableView。在将 headerView 分配给可重用使用之前直接设置 imageView 它不接受。它会给出错误:- -[UICollectionReusableView setImage:]: unrecognized selector sent to instance 0xa17be70

因为reusableView中没有像setImage这样的属性。所以准备一个带有图像的视图并作为子视图添加到可重用视图。将 headerView 分配给 reusableView 后,将 imageView 作为子视图添加到可重用视图。

   - (void)viewDidLoad
    {

        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }


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

        if (kind == UICollectionElementKindSectionHeader) {

            UIView *headerView =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

            headerView.backgroundColor=[UIColor greenColor];

            UIImageView *imageView=[[UIImageView alloc] initWithFrame:headerView.frame];
            [imageView setImage:[UIImage imageNamed:@"btn-bg.png"]];


            reusableview = (UICollectionReusableView*)headerView;
            [reusableview addSubview:imageView];
        }

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

            reusableview = footerview;
        }

        return reusableview;
    }
于 2013-06-10T04:35:38.677 回答