UICollectionView HeaderView 怎么做

发布于 2022-09-01 07:25:11 字数 69 浏览 13 评论 0

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不吻晚风 2022-09-08 07:25:11

在初始化时和添加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;
}

皓月长歌 2022-09-08 07:25:11

如果只是一个 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;
}

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文