0

プログラムで imageView を一番上に追加しようとしていますがcollectionView( 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。どんな助けでも大歓迎です!

headerViewPS これはセクション ヘッダー タイトル ビュー用ではなく、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

作業コード。subView を reusableView に追加してみてください。headerView を再利用可能な使用に割り当てる前に imageView を直接設定することは受け入れられません。次のようなエラーが発生します。

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 に答える
0

uicollectionview のデリゲート メソッドの 1 つを使用します。

-(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 に答える