ios - 从未在 viewForSupplementaryElementOfKind 中检测到 UICollectionElementKindSectionFooter

标签 ios uicollectionview swift3 uicollectionviewcell uicollectionviewlayout

我遇到一个问题,viewForSupplementaryElementOfKind 委托(delegate)函数从未检测到 UICollectionElementKindSectionFooter

因此,创建了页眉,但从未创建页脚。

我还实现了委托(delegate) referenceSizeForHeaderInSectionreferenceSizeForFooterInSection 方法。

我也注册了 Nib 。

collectionView.register(UINib(nibName: "FooterCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footerIdentifier");
collectionView.register(UINib(nibName: "HeaderCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerIdentifier");

这是不起作用的代码。

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    var view: UICollectionReusableView! = UICollectionReusableView();
    switch kind {
    case UICollectionElementKindSectionHeader:
        print("HEADER DETECTED");//CALLED
        break;
    case UICollectionElementKindSectionFooter:
        print("FOOTER DETECTED");//NEVER CALLED
        break;
    default:
        break;
    }

    return view;
}

谁能帮帮我?

P.S.: 我听说有人说在 collectionView 的布局中设置 footerReferenceSize 是可行的,但我试过了,它也不起作用

最佳答案

确保你有一个实现

func collectionView(
  _ collectionView: UICollectionView,
  layout collectionViewLayout: UICollectionViewLayout,
  referenceSizeForFooterInSection section: Int
) -> CGSize

此外,此函数应返回一个非零大小。我发现如果有任何机会返回零维,UIKit 将推断没有页脚需要出队并且不会调用您的 viewForSupplementaryElementOfKind 函数,或者 supplementaryViewProvider 如果您重新使用 iOS 13 中引入的可区分数据源。

关于ios - 从未在 viewForSupplementaryElementOfKind 中检测到 UICollectionElementKindSectionFooter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540455/

相关文章:

ios - 无法重新排序 Collection View 单元格

ios - 为 Collection View 舍入浮点值时出错

ios - Swift 中的 NSLocalizedString 注释参数

ios - 在 tableview 单元格中使用 KVO 跟踪对 Swift 3 中类实例的特定属性的更改

ios - 在 UITableViewCell 中扩展 UITextView 在底部留下间隙

iOS MDM SCEP PKIOperation : The SCEP server returned an invalid response

ios - 状态栏下的 CollectionView 标题内容

ios - 如何隐藏不在末尾的空单元格 TableView ? swift 3

html - Justify-content value of space-between 不适用于 iOS Chrome 和 Safari 浏览器

ios - UIButton 数组 - 每个按钮都指向 CollectionView swift 中的标签 0