首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios UICollectionView以编程方式删除报头

ios UICollectionView以编程方式删除报头
EN

Stack Overflow用户
提问于 2014-03-14 17:29:58
回答 2查看 5.9K关注 0票数 4

我想要控制一个UICollectionView的头,因为我需要根据用户生成的事件删除和添加它。

到目前为止,我尝试了以下几点:

代码语言:javascript
复制
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    if(toRemoveHeader){

        return CGSizeZero;

    }else{

        return CGSizeMake(320, 45);

    }
}

然后在生成user-event时调用[self.collectionView reloadData]。我更喜欢在不重新加载数据的情况下实现这一点。有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2016-02-25 23:58:41

如果你正在使用Swift,你可以在你的UICollectionViewController子类中这样做:

代码语言:javascript
复制
var hideHeader: Bool = true //or false to not hide the header

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    if hideHeader {
        return CGSizeZero //supplementary view will not be displayed if height/width are 0
    } else {
        return CGSizeMake(30,80) //size of your UICollectionReusableView
    }
}
票数 3
EN

Stack Overflow用户

发布于 2016-09-09 23:47:35

您的实现是全功能的,问题可能是您没有将实现该函数的对象分配给collectionViewdelegate属性。

函数collectionView:layout:referenceSizeForHeaderInSection:是由一个符合UICollectionViewDelegateFlowLayout协议的类实现的,collectionView期望它的delegate实现这个方法,而它的dataSource

在我的一个实现中,我只在footer中没有单元格的情况下才显示section,并且只要正确设置了delegate属性,它就可以正常工作。

代码语言:javascript
复制
#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize) collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
referenceSizeForFooterInSection:(NSInteger)section
{
    NSUInteger count = [self collectionView: collectionView
                     numberOfItemsInSection: section];
    CGFloat footerHeight = (count == 0) ? 60.f : 0.f;
    CGFloat footerWidth = collectionView.frame.size.width;
    return CGSizeMake(footerWidth, footerHeight);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22400929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档