如何为 collectionView 自定义布局中的空白部分添加补充标题视图 [英] How to add supplementary header view for an empty section in collectionView custom layout
问题描述
我有一个 UICollectionView
显示自定义布局,不是琐碎的流布局.为每个 section
项目提供补充标题.
I have a UICollectionView
that displays a custom layout, not the trivial flow layout. with supplementary headers for each section
of items.
现在,我知道这对 UX 意义不大,但出于充分的理由,我需要为 empty 部分添加补充标题.问题是,除非该部分中有项目,否则 iOS 不会显示标题.
Now, I know this doesn't make a lot of UX sense, but for good reasons I need to add a supplementary header for an empty section. The thing is, iOS doesn't display the header unless it has items in the section.
对于这个特定问题,我遇到了一些建议,但是,它们都适用于 UICollectionViewFlowLayout
,并且我正在处理自定义布局.
I have come across some suggestions to this particular matter, however, they all apply to a UICollectionViewFlowLayout
, and I am dealing with a custom layout.
有人对此问题有有效的解决方案吗?
Anyone with an efficient solution for this matter?
推荐答案
如果实现了UICollectionViewDelegateFlowLayout
协议,除了UICollectionViewDelegate
和UICollectionViewDataSource
code> 协议,即使没有项目,它也会触发所需的方法来调整大小和显示页眉和页脚视图.
If you implement the protocol UICollectionViewDelegateFlowLayout
, in addition to the UICollectionViewDelegate
and UICollectionViewDataSource
protocols, it will trigger the desired methods to size, and display, the header and footer views, even when there are no items.
class YourViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
...
}
您不需要实现任何 UICollectionViewDelegateFlowLayout
方法,只需让您的类实现协议即可.
You to not need to implement any of the UICollectionViewDelegateFlowLayout
methods, just have your class implement the protocol.
这篇关于如何为 collectionView 自定义布局中的空白部分添加补充标题视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!