zoukankan      html  css  js  c++  java
  • UICollectionView添加 HeaderView FooterView

    UICollectionView显示HeaderView FooterView 不如UITableView那么容易,常用会有两种做法:

    1.Xib或者Storyboard 在属性一栏中设置一下:

    75D44C55-6F45-4F99-BBAD-C70CC6668540.png
    如图所示,

    2.代码设计Section的header和Footer:

    好多都在找UICollectionView是否有这么个属性,比如上图说到Accessories什么的,其实不然。大家首先要搞明白意见事情,header和footer是追加视图,属于layout中的所以要代码设置section要在UICollectionViewFlowLayout:

    - (UICollectionViewFlowLayout *) flowLayout{
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.。。。。。。//各属性设置
        flowLayout.headerReferenceSize = CGSizeMake(320.0f, 50.0f);  //设置headerView大小
        flowLayout.footerReferenceSize = CGSizeMake(320.0f, 50.0f);  // 设置footerView大小
        return flowLayout;
    }   
    
    如果你用的是Xib或者Storyboard,不想在上图属性设置,想用代码:
    - (void)viewDidLoad 
    {
        [super viewDidLoad];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.headerReferenceSize = CGSizeMake(320.0f, 50.0f);  //设置headerView大小
        [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
        [self.collectionView setCollectionViewLayout:layout];
      //(这部分说明可以参见xib设置sectionview)
    }
    
    - (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WINSIZE.width, WINSIZE.width*7/15)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[UMOnlineConfig getConfigParams:@"GuizeImageUrl"]] placeholderImage:kDefaultImage192_124];
        [headerView addSubview:imageView];
        return headerView;
    }
    
    

    运行结果:

    C69DD622-3F70-432E-900E-60E76E181012.png

  • 相关阅读:
    bzoj 3339 莫队
    E. XOR and Favorite Number
    HDU 2222 AC自动机
    SPOJ 694 不同子串个数
    Codeforces Round #428 (Div. 2)
    HDU 6103
    《贪婪的动态规划》
    《浅谈图论模型的建立与应用》
    bzoj 2194 快速傅里叶之二
    java中高级面试题整理及参考答案
  • 原文地址:https://www.cnblogs.com/jyking/p/6737281.html
Copyright © 2011-2022 走看看