【问题标题】:What to return from collectionView(_:viewForSupplementaryElementOfKind:at:) when you want to return nothing?当您什么都不返回时,从 collectionView(_:viewForSupplementaryElementOfKind:at:) 返回什么?
【发布时间】:2017-06-30 09:23:41
【问题描述】:

我有一个 UICollectionView,它有节页眉,但没有节页脚。因此,我没有定义的页脚视图。 Apple's documentation 声明 You must not return nil from this method.

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    switch kind {
    case UICollectionElementKindSectionHeader:
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                                                                         withReuseIdentifier: "MyHeaderView",
                                                                         for: indexPath) as! MyHeaderView
        switch indexPath.section
        {
        case 0:
            headerView.label_title.text = "SOME HEADER"
        case 1:
            headerView.label_title.text = "ANOTHER HEADER"
        default:
            headerView.label_title.text = "UNKNOWN HEADER"
        }
        return headerView
    default:
        assert(false, "Unexpected element kind") // Not a good idea either
    }
    return nil // NOPE, not allowed
}

【问题讨论】:

  • 这样返回空的UICollectionReusableView()
  • @Dharma NOPE,它会崩溃!

标签: ios swift uicollectionview


【解决方案1】:

以上都不适合我。在我的情况下,我在同一个视图控制器中有两个 UICollectionView 对象。首先是水平的,选择其中一个项目显示下面的UICollectionView,它是垂直的并且包含节标题。

来自Apple docs

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

所以像往常一样将headerView 出列,因为如果你不这样做并且只返回一个UICollectionReusableView() 的实例,Xcode 会抱怨标题视图没有出列。然后,如果您出于某种原因不想显示它(对我来说,直到用户从上部水平集合视图中选择一个项目) - 将 headerView 的宽度和高度设置为 0.0 并返回它。

let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: collectionHeaderReuseIdentifier, for: indexPath) 
if objects.isEmpty {
    headerView.frame.size.height = 0.0
    headerView.frame.size.width = 0.0
    return headerView
}
// Configure the header view here if needed
return headerView

【讨论】:

    【解决方案2】:

    就我个人而言,如果您没有告诉 UICollectionView 显示部分页脚,那么它就不会显示它们。所以可以把preconditionFailure放到那个委托方法中。

    我的想法是,UICollectionView 不会做更多你告诉它做的事情。因此,如果您没有告诉 UICollectionView 显示部分页脚,那么它将不会显示它们,并且对于这些页脚视图情况,使用 preconditionFailure 将是安全的。但是,如果您在这种情况下发现崩溃,则要么是 UIKit 错误,您应该提交雷达通知 Apple,或者是您的错误要求 UICollectionView 无意中显示节页脚。

    所以我的代码看起来与此类似

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
      switch kind {
      case UICollectionElementKindSectionHeader:
          let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                                                                         withReuseIdentifier: "MyHeaderView",
                                                                         for: indexPath) as! MyHeaderView
          /* Configure your section header here */
          return headerView
      default:
          preconditionFailure("Invalid supplementary view type for this collection view")
      }
    }
    

    【讨论】:

      【解决方案3】:

      尝试如下

      func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
             var headerView = UICollectionReusableView()
          if kind == UICollectionElementKindSectionHeader {
              headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderViews", for: indexPath)
      
          }
          return headerView
      

      【讨论】:

      • 这将导致其他部分崩溃,因为您正在强制包装headerview
      • @NiravD 谢谢。我忘记了。现在我更新我的答案。
      【解决方案4】:

      如您所见,viewForSupplementaryElementOfKind 方法返回 UICollectionReusableView,它不是 可选类型,因此您必须初始化 UICollectionReusableView 对象并返回该对象如下面的例子,当你不想返回任何东西时。

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

      【讨论】:

        【解决方案5】:

        我猜你可以返回空视图。它不会指定任何大小。

         retun UIView()
        

        【讨论】:

          【解决方案6】:

          尝试使用

          return UICollectionReusableView()
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-18
            • 2018-09-10
            • 2017-02-14
            • 2013-10-25
            • 1970-01-01
            相关资源
            最近更新 更多