我想要控制一个UICollectionView的头,因为我需要根据用户生成的事件删除和添加它。
到目前为止,我尝试了以下几点:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
if(toRemoveHeader){
return CGSizeZero;
}else{
return CGSizeMake(320, 45);
}
}然后在生成user-event时调用[self.collectionView reloadData]。我更喜欢在不重新加载数据的情况下实现这一点。有什么想法吗?
发布于 2016-02-25 23:58:41
如果你正在使用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
}
}发布于 2016-09-09 23:47:35
您的实现是全功能的,问题可能是您没有将实现该函数的对象分配给collectionView的delegate属性。
函数collectionView:layout:referenceSizeForHeaderInSection:是由一个符合UICollectionViewDelegateFlowLayout协议的类实现的,collectionView期望它的delegate实现这个方法,而不它的dataSource。
在我的一个实现中,我只在footer中没有单元格的情况下才显示section,并且只要正确设置了delegate属性,它就可以正常工作。
#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);
}https://stackoverflow.com/questions/22400929
复制相似问题