【问题标题】:Adding An Image View to the Top of a Collection View?将图像视图添加到集合视图的顶部?
【发布时间】:2013-06-09 23:59:01
【问题描述】:

我正在尝试以编程方式将 imageView 添加到我的collectionView 的顶部(类似于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 工作。任何帮助将不胜感激!

附:这不适用于 Section Header Title View,它类似于 Apple App Store 中的headerView

更新:

我还在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;
}

【问题讨论】:

    标签: ios objective-c uiimageview uicollectionview


    【解决方案1】:

    工作代码。尝试将 subView 添加到 reusableView。在将 headerView 分配给可重用使用之前直接设置 imageView 不接受。它会给出错误:- -[UICollectionReusableView setImage:]: 无法识别的选择器发送到实例 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;
        }
    

    【讨论】:

      【解决方案2】:

      使用 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;
      }
      

      【讨论】:

      • 嗨格伦!不过,我想知道这一点。如果我已经将 viewForSupplementaryElementOfKind 用于我的部分标题,这会起作用吗?换句话说,如果我想要一个节标题和一个视图标题,这会起作用吗? PS我已经更新了我的答案,以显示到目前为止我为我的部分标题所做的工作。
      • 是的..只是或更好地创建一个自定义 UICollectionReusableView 子类
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多