ios - UICollectionView HeaderView 怎么做
ringa_lee
ringa_lee 2017-04-17 14:44:55
[iOS讨论组]

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

ringa_lee
ringa_lee

ringa_lee

全部回复(2)
ringa_lee

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

大家讲道理

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

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

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号