ios UICollectionView:如何获取部分的标题视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13394282/
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
UICollectionView: How to get the header view for a section?
提问by Dorian Roy
There is a method to get a cell by indexPath
(UICollectionView cellForItemAtIndexPath:
). But I can"t find a method to get one of the supplementary views like a header or footer, after it has been created. Any ideas?
有一种方法可以通过indexPath
( UICollectionView cellForItemAtIndexPath:
)获取单元格。但是,在创建后,我找不到一种方法来获取诸如页眉或页脚之类的补充视图之一。有任何想法吗?
回答by rob mayoff
UPDATE
更新
As of iOS 9, you can use -[UICollectionView supplementaryViewForElementKind:atIndexPath:]
to get a supplementary view by index path.
从 iOS 9 开始,您可以使用-[UICollectionView supplementaryViewForElementKind:atIndexPath:]
通过索引路径获取补充视图。
ORIGINAL
原来的
Your best bet is to make your own dictionary mapping index paths to supplementary views. In your collectionView:viewForSupplementaryElementOfKind:atIndexPath:
method, put the view into the dictionary before returning it. In your collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:
, remove the view from the dictionary.
最好的办法是制作自己的字典,将索引路径映射到补充视图。在您的collectionView:viewForSupplementaryElementOfKind:atIndexPath:
方法中,在返回之前将视图放入字典中。在您的 中collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:
,从字典中删除视图。
回答by Bluezen
I would like to share my insight of the solution provided by rob mayoffbut I can't post comment so I am putting it here:
我想分享我对rob mayoff提供的解决方案的见解,但我无法发表评论,所以我把它放在这里:
For every one of you that tried to keep reference of the supplementary views being used by a collection view but who run into issues of loosing track too early because of
对于那些试图保留集合视图正在使用的补充视图的参考但由于过早地失去跟踪的问题的每个人
collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:
being called too many times, try using an NSMapTable instead of a dictionary.
被调用太多次,尝试使用 NSMapTable 而不是字典。
I use
我用
@property (nonatomic, strong, readonly) NSMapTable *visibleCollectionReusableHeaderViews;
created like this:
像这样创建:
_visibleCollectionReusableHeaderViews = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];
so that when you are keeping a reference to a supplementary view:
这样当您保留对补充视图的引用时:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
// ( ... )
[_visibleCollectionReusableHeaderViews setObject:cell forKey:indexPath];
it keeps only a WEAK reference to it in the NSMapTable and it keeps it AS LONG AS the object is not deallocated!
它只在 NSMapTable 中保留对它的弱引用,并且只要对象没有被释放,它就会保留它!
You don't need anymore to remove the view from
您不再需要从中删除视图
collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:
as the NSMapTable will lose the entry as soon as the view is deallocated.
因为一旦视图被释放,NSMapTable 就会丢失条目。
回答by Alex
First thing you have to do is check the box "Section Header" in the collection view's attribute inspector. Then add a collection reusable view just like you added your cell to the collection view, write an identifier and make a class for it if you need to. Then implement the method:
您要做的第一件事是在集合视图的属性检查器中选中“Section Header”框。然后添加一个集合可重用视图,就像您将单元格添加到集合视图一样,编写一个标识符并在需要时为它创建一个类。然后实现方法:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
From there do exactly like you did with cellForItemAtIndexPath Its also important to specify if its a header or footer you are coding about:
从那里做就像你对 cellForItemAtIndexPath 所做的一样 指定它是你编码的页眉还是页脚也很重要:
if([kind isEqualToString:UICollectionElementKindSectionHeader])
{
Header *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerTitle" forIndexPath:indexPath];
//modify your header
return header;
}
else
{
EntrySelectionFooter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"entryFooter" forIndexPath:indexPath];
//modify your footer
return footer;
}
use indexpath.section to know what section this is in also note that Header and EntrySelectionFooter are custom subclasses of UICollectionReusableView that I made
使用 indexpath.section 知道这是在哪个部分还请注意 Header 和 EntrySelectionFooter 是我制作的 UICollectionReusableView 的自定义子类
回答by ullstrm
This method is often enough to serve the purpose of reloading on-screen supplementary views:
这种方法通常足以满足重新加载屏幕补充视图的目的:
collectionView.visibleSupplementaryViews(ofKind: UICollectionElementKindSectionHeader)