Gui*_*iks 42 cocoa-touch objective-c uicollectionview
我正在开发一个UICollectionView
用来显示几张专辑的项目.项目显示正常,但现在我想在第一部分上方显示标题.
为此,我添加了registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
我的init方法.像这样:
[self.collectionView registerNib:[UINib nibWithNibName:@"AlbumHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kAlbumHeaderIdentifier];
Run Code Online (Sandbox Code Playgroud)
(AlbumHeader
Nib包含类的视图AlbumHeader
,它是.的子类UICollectionView
.)
之后,我实现了collectionView:viewForSupplementaryElementOfKind:atIndexPath
方法:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
return [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kAlbumHeaderIdentifier forIndexPath:indexPath];
}
Run Code Online (Sandbox Code Playgroud)
现在它应该尝试加载标题视图,我想.但事实并非如此,补充视图的方法不会被调用.
我错过了什么?坚持几个小时,已多次阅读文档UICollectionView
,但似乎没有任何帮助.有什么想法吗?
Gui*_*iks 115
在查找yuf询问的方法后,我读到默认情况下页眉/页脚的大小是0,0.如果大小为0,则不显示页眉/页脚.
您可以使用属性设置大小:
flowLayout.headerReferenceSize = CGSizeMake(0, 100);
Run Code Online (Sandbox Code Playgroud)
然后所有标题将具有相同的大小.如果每个部分必须不同,则可以实现以下方法,该方法是UICollectionViewDelegateFlowLayout
协议的一部分.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
if (section == albumSection) {
return CGSizeMake(0, 100);
}
return CGSizeZero;
}
Run Code Online (Sandbox Code Playgroud)
请注意,在垂直滚动中,它使用height
集合视图的返回和整个宽度,在水平滚动时,它使用集合视图的返回width
和完整高度.
归档时间: |
|
查看次数: |
38539 次 |
最近记录: |