给UICollectionView添加表头

来源:互联网 发布:模具分析软件 编辑:程序博客网 时间:2024/06/09 19:02

//要先设置表头高度

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

{

    

    CGSize size = {SCREEN_WIDTH,50};

    return size;

    

}


//在表头内添加内容,需要创建一个继承collectionReusableView的类,用法类比tableViewcell

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

{

    // 注册表头

    [collectionView registerClass:[MCClassifyCollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"HeaderView"];

    

    // 初始化表头

    MCClassifyCollectionReusableView *headerView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"HeaderView"forIndexPath:indexPath];

    NSArray * array =@[@"1",@"2",@"3",@"4",@"5",@"6",@"7"];

    headerView.hotSingerName.text = array[indexPath.section];

    

    return headerView;

}


1 1