ios collectionView:viewForSupplementaryElementOfKind:atIndexPath: 仅使用 UICollectionElementKindSectionHeader 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13191480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
collectionView:viewForSupplementaryElementOfKind:atIndexPath: called only with UICollectionElementKindSectionHeader
提问by Stelian Iancu
I have a collection view and I would like each section to have both a header and a footer. I'm using the default flow layout.
我有一个集合视图,我希望每个部分都有一个页眉和一个页脚。我正在使用默认的流程布局。
I have my own subclasses of UICollectionReusableView
and I register each for both the header and the footer in the viewDidLoad
method of my view controller.
我有自己的子类,UICollectionReusableView
我在viewDidLoad
视图控制器的方法中为页眉和页脚注册了每个子类。
I've implemented the method collectionView:viewForSupplementaryElementOfKind:atIndexPath:
but, for each section, it is only called with kind
being UICollectionElementKindSectionHeader
. Therefore my footer isn't even created.
我已经实现了方法collectionView:viewForSupplementaryElementOfKind:atIndexPath:
,但是,对于每一个部分,它只是调用kind
是UICollectionElementKindSectionHeader
。因此我的页脚甚至没有被创建。
Any ideas why this happens?
任何想法为什么会发生这种情况?
回答by Stelian Iancu
It seems that I have to set the footerReferenceSize
for the collection view layout. Weird that I didn't have to do that with the header.
看来我必须footerReferenceSize
为集合视图布局设置 。奇怪的是,我不必对标题这样做。
回答by DawnSong
(Using Swift 3.1, Xcode 8.3.3)
First, register header's class or nib
(使用 Swift 3.1, Xcode 8.3.3)
首先,注册头的类或笔尖
collectionView.register(ShortVideoListHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
Second, set headerReferenceSize
; alternatively, you can return headerReferenceSize
in collectionView's delegate
二、设置headerReferenceSize
;或者,您可以headerReferenceSize
在 collectionView 的委托中返回
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
(collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.headerReferenceSize = CGSize(width: view.bounds.width, height: 156)
}
Third, write your own header class, such as,
三、编写自己的头类,如,
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
}
}
Fourth, return your header instance in collectionView's dataSource methods,
第四,在 collectionView 的 dataSource 方法中返回您的标头实例,
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()
}
}
By the way, Apple's Documentanswers how to hide section header.
顺便说一句,Apple 的文档回答了如何隐藏节标题。
This method must always return a valid view object. If you do not want a supplementary view in a particular case, your layout object should not create the attributes for that view. Alternatively, you can hide views by setting the hidden property of the corresponding attributes to YES or set the alpha property of the attributes to 0. To hide header and footer views in a flow layout, you can also set the width and height of those views to 0.
此方法必须始终返回有效的视图对象。如果在特定情况下不需要补充视图,则布局对象不应为该视图创建属性。或者,您可以通过将相应属性的 hidden 属性设置为 YES 或将属性的 alpha 属性设置为 0 来隐藏视图。 要在流布局中隐藏页眉和页脚视图,您还可以设置这些视图的宽度和高度到 0。
回答by Kinphi
I found some code maybe can help you
我发现一些代码也许可以帮助你
- ( 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;
}