CollectionView Header And Footer 的使用


CollectionView Header And footer 的添加和設置

建議先看CollectionView 第一篇:http://www.jianshu.com/p/a1614404ae96

 

注冊Header 和 Footer

    //注冊區頭

        [_myCollectionView registerClass:[HeaderCRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];

   //注冊區尾

        [_myCollectionView registerClass:[HeaderCRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];

 

自定義Header子類  ---->HeaderCRView (創建)

自定義的話,header子類要繼承自 UICollectionReusableView。

#pragma mark 自定義區頭  區尾 樣式

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

    NSString *CellIdentifier = @"headerView";

    NSString *cellfooter = @"footerView";

    

    HeaderCRView *cell= nil;

 

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

    

    

         cell = (HeaderCRView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellIdentifier forIndexPath:indexPath];

        [cell initCollectionHeaderViewindex:indexPath.section];

 

        

    }

     if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {

    

        

         cell = (HeaderCRView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:cellfooter forIndexPath:indexPath];

        

        [cell initCollectionFootViewindex:indexPath.section];

    }

    

    return cell;

}

 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

        //設置區頭高度

    if (section == 0) {

        CGSize size = CGSizeMake(kUIScreenWidth, 135+35);

        return size;

    }

    CGSize size = CGSizeMake(kUIScreenWidth, 35);

    return size;

 

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {

    //設置區尾 高度

    CGSize size = CGSizeMake(kUIScreenWidth, 10);

    return size;

    

}

 

HeaderCRView  .h

@interface HeaderCRView : UICollectionReusableView

 

//區頭

- (void)initCollectionHeaderViewindex:(NSInteger *)index;

//區尾

- (void)initCollectionFootViewindex:(NSInteger *)indexPath;

 

@end

 

HeaderCRView  .m

- (void)initCollectionHeaderViewindex:(NSInteger *)index {

    

    if (index == 0) {

        

        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 135, kUIScreenWidth, 35)];

        headerView.backgroundColor = [UIColor whiteColor];

        [self addSubview:headerView];

        

        UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 80, 15)];

        titleLab.text = @"我的應用";

        titleLab.textColor = [CommonFunctions colorWithHex:0x4A4A4A];

        titleLab.font = [UIFont fontWithName:kPingFang_Regular size:14];

        [headerView addSubview:titleLab];

        

    }

    

    if (index == 1) {

        

        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 35)];

        headerView.backgroundColor = [UIColor whiteColor];

        [self addSubview:headerView];

        

        UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 80, 15)];

        titleLab.text = @"業務發展";

        titleLab.textColor = [CommonFunctions colorWithHex:0x4A4A4A];

        titleLab.font = [UIFont fontWithName:kPingFang_Regular size:14];

        [headerView addSubview:titleLab];

        

    }

    

}

 

- (void)initCollectionFootViewindex:(NSInteger *)indexPath {

    

    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 10)];

    footView.backgroundColor = [UIColor groupTableViewBackgroundColor];

    [self addSubview:footView];

    

    

}

 

 

 

效果圖 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM