ios 添加一个简单的 UIView 作为 UICollectionView 的标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21731318/
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
Add a simple UIView as header of UICollectionView
提问by Nicolas Roy
I have a UICollectionView
. I would like to add a header. My header would only be a UILabel
. I've :
我有一个UICollectionView
. 我想添加一个标题。我的标题只会是一个UILabel
. 我有 :
1) selected "Section Header" as a Collection View accessory in the IB.
1)在IB中选择“Section Header”作为集合视图附件。
2) created a Collection Reusable View in the IB, on the side, declared as collectionViewHeader
.
2)在IB中创建了一个Collection Reusable View,在旁边,声明为collectionViewHeader
。
3) added those lines :
3)添加了这些行:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
return collectionViewHeader;
}
return nil;
}
But they are never called.
但他们永远不会被调用。
Do I have to create a class just for that label in order to use
我是否必须为该标签创建一个类才能使用
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
?
?
Why isn't it as simple as the UITableView
where you just drag and drop whatever header you want ?! Things are so complicated with UICollectionView
...
为什么不像UITableView
您只需拖放您想要的任何标题那么简单?!事情是如此复杂UICollectionView
...
回答by Eike
In swift like below
像下面这样快速
Register Header View
注册标题视图
collectionView.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView")
In UICollectionViewDataSource
在 UICollectionViewDataSource
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView", forIndexPath: indexPath)
headerView.frame.size.height = 100
return headerView
}
Important is that you are supply the flow layout with the header size
重要的是您要提供带有标题大小的流布局
flowLayout.headerReferenceSize = CGSize(width: self.collectionView.frame.size.width, height: 100)
Otherwise the data source method will not get called
否则不会调用数据源方法
回答by Raz
If you don't set the header view in storyboard, you will have to register nib.
如果您没有在 storyboard 中设置标题视图,则必须注册 nib。
Example in viewDidLoad:
viewDidLoad 中的示例:
- (void)viewDidLoad
{
[self.collectionView registerClass:NSStringFromClass([YourOwnSubClass class]) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewIdentifier"];
}
Anyway, you can also subclass UICollectionReusableView.
不管怎样,你也可以继承 UICollectionReusableView。
@interface YourOwnSubClass : UICollectionReusableView
then call delegate in your class example:
然后在您的类示例中调用委托:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
YourOwnSubClass *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
[self updateSectionHeader:headerView forIndexPath:indexPath];
return headerView;
}
- (void)updateSectionHeader:(UICollectionReusableView *)header forIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [NSString stringWithFormat:@"header #%i", indexPath.row];
header.label.text = text;
}
And don't forget to set header size in collection view or in flow layout so header view will be visible.
并且不要忘记在集合视图或流布局中设置标题大小,以便标题视图可见。