UICollectionHeaderView

我們都知道UITableView有個(gè)ViewForHeaderInSection
然后我們?cè)谧鯱ICollectionView的時(shí)候同樣需要一個(gè)SectionHeaderView

首先需要自定義一個(gè)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去注冊(cè), 并實(shí)現(xiàn)代理方法, 重點(diǎn):必須在Layout的HeaderReferenceSize里給個(gè)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有所變化的話就必須實(shí)現(xiàn)代理方法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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末嘀韧,一起剝皮案震驚了整個(gè)濱河市锭汛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖解虱,帶你破解...
    沈念sama閱讀 216,692評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異咙轩,居然都是意外死亡苞慢,警方通過查閱死者的電腦和手機(jī)蟹演,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門风钻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人酒请,你說我怎么就攤上這事魄咕。” “怎么了蚌父?”我有些...
    開封第一講書人閱讀 162,995評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵哮兰,是天一觀的道長(zhǎng)毛萌。 經(jīng)常有香客問我,道長(zhǎng)喝滞,這世上最難降的妖魔是什么阁将? 我笑而不...
    開封第一講書人閱讀 58,223評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮右遭,結(jié)果婚禮上做盅,老公的妹妹穿的比我還像新娘。我一直安慰自己窘哈,他們只是感情好吹榴,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著滚婉,像睡著了一般图筹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上让腹,一...
    開封第一講書人閱讀 51,208評(píng)論 1 299
  • 那天远剩,我揣著相機(jī)與錄音,去河邊找鬼骇窍。 笑死瓜晤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的腹纳。 我是一名探鬼主播痢掠,決...
    沈念sama閱讀 40,091評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼嘲恍!你這毒婦竟也來了志群?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,929評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤蛔钙,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后荠医,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吁脱,經(jīng)...
    沈念sama閱讀 45,346評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評(píng)論 2 333
  • 正文 我和宋清朗相戀三年彬向,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了兼贡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,739評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡娃胆,死狀恐怖遍希,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情里烦,我是刑警寧澤凿蒜,帶...
    沈念sama閱讀 35,437評(píng)論 5 344
  • 正文 年R本政府宣布禁谦,位于F島的核電站,受9級(jí)特大地震影響废封,放射性物質(zhì)發(fā)生泄漏州泊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評(píng)論 3 326
  • 文/蒙蒙 一漂洋、第九天 我趴在偏房一處隱蔽的房頂上張望遥皂。 院中可真熱鬧,春花似錦刽漂、人聲如沸演训。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽样悟。三九已至,卻和暖如春颈畸,著一層夾襖步出監(jiān)牢的瞬間乌奇,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工眯娱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留礁苗,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,760評(píng)論 2 369
  • 正文 我出身青樓徙缴,卻偏偏與公主長(zhǎng)得像试伙,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子于样,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容