Ios collectionView:ViewForSupplementElementOfKind:atIndexPath:仅使用UICollectionElementKindSectionHeader调用
我有一个集合视图,我希望每个部分都有页眉和页脚。我使用的是默认的流布局 我有自己的Ios collectionView:ViewForSupplementElementOfKind:atIndexPath:仅使用UICollectionElementKindSectionHeader调用,ios,ios6,Ios,Ios6,我有一个集合视图,我希望每个部分都有页眉和页脚。我使用的是默认的流布局 我有自己的UICollectionReusableView子类,我在视图控制器的viewDidLoad方法中为页眉和页脚注册了每个子类 我已经实现了方法collectionView:viewforSupplementElementOfKind:atIndexPath:,但是对于每个部分,它只在kind为UICollectionElementKindSectionHeader时调用。因此,我的页脚甚至没有被创建 知道为什么会发
UICollectionReusableView
子类,我在视图控制器的viewDidLoad
方法中为页眉和页脚注册了每个子类
我已经实现了方法collectionView:viewforSupplementElementOfKind:atIndexPath:
,但是对于每个部分,它只在kind
为UICollectionElementKindSectionHeader时调用。因此,我的页脚甚至没有被创建
知道为什么会发生这种情况吗?似乎我必须为集合视图布局设置footereferencesize
。奇怪的是,我不必对标题这么做。我发现一些代码可能可以帮助您
- ( UICollectionReusableView * ) collectionView : ( UICollectionView * ) collectionView viewForSupplementaryElementOfKind : ( NSString * ) kind atIndexPath : ( NSIndexPath * ) indexPath
{
UICollectionReusableView * reusableview = nil ;
if ( kind == UICollectionElementKindSectionHeader )
{
RecipeCollectionHeaderView * headerView = [ collectionView dequeueReusableSupplementaryViewOfKind : UICollectionElementKindSectionHeader withReuseIdentifier : @ "HeaderView" forIndexPath : indexPath ] ;
NSString * title = [ [ NSString alloc ] initWithFormat : @ "Recipe Group #%i" , indexPath.section + 1 ] ;
headerView.title.text = title;
UIImage * headerImage = [ UIImage imageNamed : @ "header_banner.png" ] ;
headerView.backgroundImage.image = headerImage;
reusableview = headerView;
}
if ( kind == UICollectionElementKindSectionFooter )
{
UICollectionReusableView * footerview = [ collectionView dequeueReusableSupplementaryViewOfKind : UICollectionElementKindSectionFooter withReuseIdentifier : @ "FooterView" forIndexPath : indexPath ] ;
reusableview = footerview;
}
return reusableview;
}
(使用Swift 3.1,Xcode 8.3.3)
首先,注册头的类或nib
collectionView.register(ShortVideoListHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
Second,设置headerReferenceSize
;或者,您可以在collectionView的委托中返回headerReferenceSize
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
(collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.headerReferenceSize = CGSize(width: view.bounds.width, height: 156)
}
第三个,编写自己的头类,例如
class ShortVideoListHeader: UICollectionReusableView {
let titleLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(titleLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel.sizeToFit()
titleLabel.frame.origin = CGPoint(x: 15, y: 64 + (frame.height - 64 - titleLabel.frame.height) / 2) // navigationBar's height is 64
}
}
第四个,在collectionView的数据源方法中返回头实例
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath) as! ShortVideoListHeader
header.titleLabel.text = title
header.setNeedsLayout()
return header
default:
return UICollectionReusableView()
}
}
顺便说一下,回答了如何隐藏节标题
此方法必须始终返回有效的视图对象。如果在特定情况下不需要辅助视图,则布局对象不应为该视图创建属性。或者,可以通过设置
隐藏的
将相应属性的属性设置为“是”或设置
阿尔法
属性的属性设置为0。要隐藏流布局中的页眉和页脚视图,还可以将这些视图的宽度和高度设置为0
我也有同样的经历(页眉和页脚)-似乎如果不设置这些,它不会尝试加载页眉和页脚。老实说,API好像缺少了一个方法,比如“-(BOOL)indexPath:hasSupplementalViewOfKind:”让开发人员明确确定哪些indexPath有补充视图。如何增加页脚高度?谢谢,但问题不是这个方法的实现,正如我在回答中所说的。还要注意,在iOS 7上,如果返回nil,应用程序将崩溃。如何增加xib的高度。@KrunalNagvadia在相应的委托方法中设置另一个引用大小或返回新大小,然后重新加载节或仅重新加载集合视图。