美文网首页
UICollectionHeaderView

UICollectionHeaderView

作者: 今年27 | 来源:发表于2018-10-20 10:27 被阅读9次

    我们都知道UITableView有个ViewForHeaderInSection
    然后我们在做UICollectionView的时候同样需要一个SectionHeaderView

    首先需要自定义一个view

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface SSChoosePhotoHeaderView : UICollectionReusableView
    
    @property (nonatomic, strong)UILabel* testLabel;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "SSChoosePhotoHeaderView.h"
    
    @implementation SSChoosePhotoHeaderView
    
    -(instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, KMAIN_SCREEN_WIDTH, 44)];
            _testLabel = label;
            [self addSubview:label];
        }
        return self;
    }
    
    

    然后呢需要在CollectionView去注册, 并实现代理方法, 重点:必须在Layout的HeaderReferenceSize里给个size

    -(void)setBasicUI{
      
        CGFloat kItemWidth = (self.view.width-6)/3;
        CGFloat kItemHeight = kItemWidth;
        UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
        [layout setItemSize:CGSizeMake(kItemWidth, kItemHeight)];
        //    [layout setSectionInset:UIEdgeInsetsMake(2, 2, 2 ,2)];
        [layout setSectionInset:UIEdgeInsetsMake(1, 1, 1 , 1)];
        layout.headerReferenceSize = CGSizeMake(KMAIN_SCREEN_WIDTH, 44);
    
        
        self.collectionView = [[UICollectionView alloc]initWithFrame:(CGRect){0, 0, self.view.width, self.view.height} collectionViewLayout:layout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate=self;
        self.collectionView.dataSource=self;
        self.collectionView.alwaysBounceVertical = YES;
        [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SSChoosePhotoCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([SSChoosePhotoCollectionViewCell class])];
        [self.view addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.naviBar.mas_bottom);
            make.left.right.bottom.equalTo(self.view);
        }];
         [_collectionView registerClass:[SSChoosePhotoHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([SSChoosePhotoHeaderView class])];
        if ([self.collectionView respondsToSelector:@selector(setPrefetchingEnabled:)]) {
            self.collectionView.prefetchingEnabled = false;
        }
        //    [self.collectionView reloadData];
    }
    
    -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
        
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            SSChoosePhotoHeaderView* view = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([SSChoosePhotoHeaderView class]) forIndexPath:indexPath];
            if (view == nil) {
                view = [[SSChoosePhotoHeaderView alloc] initWithFrame:CGRectMake(0, 0, KMAIN_SCREEN_WIDTH, 44)];
            }
            JSDateModel* model = _itemsArray[indexPath.section];
            view.testLabel.text = model.dateString;
            return view;
        }
        return nil;
        
    }
    

    接下来就是如果headsize有所变化的话就必须实现代理方法UICollectionViewDelegateFlowLayout

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;//item尺寸
    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;//section Inset
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;//行间距
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;//列间距
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;//head size
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;//footer size
    

    相关文章

      网友评论

          本文标题:UICollectionHeaderView

          本文链接:https://www.haomeiwen.com/subject/dfhuzftx.html