首页 > UICollectionView HeaderView 怎么做

UICollectionView HeaderView 怎么做

如题。我希望UICollectionView中实现类似于UITableView的tableHeaderView,怎么做啊


如果只是一个 header 一个 footer,实现- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath即可。

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader) {
        reusableview = someHeaderView;
    }

    if (kind == UICollectionElementKindSectionFooter) {
        reusableview = someFooterView;
    }

    return reusableview;
}

如果是要保持停留在顶部的效果,没有直接能设置的办法,只能自己实现。

有一些第三方库可以实现,也可以参考这里。


在初始化时和添加cell类似
[collectionView registerClass:[HeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeadID];
再加楼上的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
HeadView * view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeadID forIndexPath:indexPath];
return view;
}
return nil;
}

【热门文章】
【热门文章】