添加collection的header,footer

   UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

 [layout setScrollDirection:UICollectionViewScrollDirectionVertical];

   layout.headerReferenceSize = CGSizeMake(100, 22);//写上该行代码后,显示header和footer的回调方法成功执行,最终成功显示header

 

[dataListcollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];//注册header的view

 

回调方法

 //显示header和footer的回调方法

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

{

//如果想要自定义header,只需要定义UICollectionReusableView的子类A,然后在该处使用,注意AIdentifier要设为注册的字符串,此处为“header”

    if (kind == UICollectionElementKindSectionHeader)

 {       

    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];

        return view;

    }

    return nil;

}

 

posted on 2017-11-14 14:27  _MaL  阅读(324)  评论(0)    收藏  举报