ios UICollectionView 以编程方式删除标题

Pet*_*tar 6 ios uicollectionview uicollectionviewlayout

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

到目前为止我已经尝试过:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    if(toRemoveHeader){

        return CGSizeZero;

    }else{

        return CGSizeMake(320, 45);

    }
}
Run Code Online (Sandbox Code Playgroud)

然后[self.collectionView reloadData]每当生成用户事件时调用。我更愿意在不重新加载数据的情况下完成此操作。有任何想法吗?

gam*_*ill 5

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)