程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了可选的 UICollectionView 标题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决可选的 UICollectionView 标题?

开发过程中遇到可选的 UICollectionView 标题的问题如何解决?下面主要结合日常开发的经验,给出你关于可选的 UICollectionView 标题的解决方法建议,希望对你解决可选的 UICollectionView 标题有所启发或帮助;

我在 UICollectionVIEwDatasource 中实现了以下方法,以便为我的 UICollectionVIEw 部分返回标题。

但是,如果 header 为真,我只想返回 hasOthers 视图。在这种情况下,以下代码会引发以下异常:

Exception NSException * "the vIEw returned from -collectionVIEw:vIEwForSupplementaryElementOfKind:aTindexPath (UICollectionElementKindSectionheader,<NSIndexPath: 0xaa02564b411fe771> {length = 2,path = 0 - 0}) was not retrIEved by calling -dequeueReusableSupplementaryVIEwOfKind:withReusEIDentifIEr:forIndexPath: or is nil (<UICollectionReusableVIEw: 0x7ff43f577700; frame = (0 0; 0 0); layer = <CALayer: 0x6000026484a0>>)" 0x000060000321f240

我也无法从此方法返回 nil,因为它不返回 Optional

func collectionVIEw(_ collectionVIEw: UICollectionVIEw,vIEwForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableVIEw {
    if kind == UICollectionVIEw.elementKindSectionheader {
        if !hasOthers {
            return UICollectionReusableVIEw()
        }
        if let sectionheader = collectionVIEw.dequeueReusableSupplementaryVIEw(ofKind: kind,withReusEIDentifIEr: "header",for: indexPath) as? OrderCVheaderVIEw {
            sectionheader.BACkgroundcolor = .white
            return sectionheader
        }
    }
    return UICollectionReusableVIEw()
}

我在实施时遵循了这个例子:

https://stackoverflow.com/a/57666316/4909000

仅当 hasOthers 为 true 时,我如何才能返回可选标头?

解决方法

文档说:此方法必须始终返回有效的视图对象。如果在特定情况下不需要补充视图,则布局对象不应为该视图创建属性。或者,您可以通过将相应属性的 isHidden 属性设置为 true 或将属性的 alpha 属性设置为 0 来隐藏视图。 要在流布局中隐藏页眉和页脚视图,您还可以设置这些视图的宽度和高度到 0。

大佬总结

以上是大佬教程为你收集整理的可选的 UICollectionView 标题全部内容,希望文章能够帮你解决可选的 UICollectionView 标题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。