简体   繁体   English

如何有一个视图 controller 有两个集合视图,但只有一个有页眉/页脚视图?

[英]How to have a view controller with two collection views, but only one has header/footer views?

I have two collection views in my view controller, which is set as the delegate and data source for both.我的视图 controller 中有两个集合视图,它们被设置为两者的委托和数据源。 One of those collection views has a registered supplementary view as its header, which it dequeues and displays properly before I added the second collection view.其中一个集合视图有一个注册的补充视图作为它的 header,它在我添加第二个集合视图之前将其出列并正确显示。 Now with the second collection view, the viewForSupplementaryElementOfKind causes an error since the second collection view doesn't have any registered headers.现在使用第二个集合视图, viewForSupplementaryElementOfKind会导致错误,因为第二个集合视图没有任何注册的标头。 How can I ensure that the function gets called only on the first collection view?如何确保仅在第一个集合视图上调用 function?

Create a simple and short custom subclass for a UICollectionView like this:为 UICollectionView 创建一个简单而简短的自定义子类,如下所示:

class CustomCollectionView : UICollectionView {
    let showFooter : Bool
    let showHeader : Bool

    init(showHeader:Bool,showFooter:Bool) {
        self.showFooter = showFooter
        self.showHeader = showHeader
        //Create a custom UICollectionViewLayout and frame according to your requirement here (You can send parent ViewControllers frame as a param in the init above too)
        super.init(frame: CGRect.zero, collectionViewLayout: UICollectionViewLayout.init())
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Now just initialise the first CollectionView with showHeader and/or showFooter as true and other accordingly.You would now need to do the following in the delegate callback that asks for a supplementary view:现在只需使用 showHeader 和/或 showFooter 将第一个 CollectionView 初始化为 true 和其他相应的。您现在需要在请求补充视图的委托回调中执行以下操作:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    guard let collectionView = collectionView as? CustomCollectionView else {
        return UICollectionReusableView.init(frame: .zero)
    }

    switch kind {
    case UICollectionView.elementKindSectionFooter:
        if collectionView.showFooter {
                //Dequeue a footer and return
        }
    case UICollectionView.elementKindSectionHeader:
        if collectionView.showHeader {
                //Dequeue a header and return
        }
    default:
        return UICollectionReusableView.init(frame: .zero)
    }
    return UICollectionReusableView.init(frame: .zero)
}

The first parameter to the various data source and delegate methods is the collection view.各种数据源和委托方法的第一个参数是集合视图。

I do not recommend using view tags as the other poster suggested.我不建议像其他海报建议的那样使用视图标签。 That approach is fragile.这种方法很脆弱。

Instead, have your view controller keep pointers to each and then have the methods check which one is being called:相反,让您的视图 controller 保留指向每个的指针,然后让方法检查正在调用哪个:

@IBOulet collectionView1: UICollectionView!
@IBOulet collectionView2: UICollectionView!

Then in your viewForSupplementaryElementOfKind method:然后在您的 viewForSupplementaryElementOfKind 方法中:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    switch collectionView {
        case collectionView1:
            //code to return a view for collection view 1
        case collectionView2:
            //code to return a view for collection view 2
        default: 
            //Can't happen, but keep the compiler happy:
            return UIView(frame: CGRect.zeroRect)
    }
}

Alternately, set up separate objects to serve as the data sources of each collection view.或者,设置单独的对象作为每个集合视图的数据源。 Have your view controller create the data sources and hook them up in its viewDidLoad method.让您的视图 controller 创建数据源并将它们连接到其 viewDidLoad 方法中。

If one collection view has a header and the other does not, you need to configure your layout objects separately for each collection view.如果一个集合视图具有 header 而另一个没有,则需要为每个集合视图单独配置布局对象。 (You need to have code like the above that handles the two collection views separately for ALL of your collection view methods.) (您需要有类似上面的代码,为您的所有集合视图方法分别处理两个集合视图。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个View Controller中拥有多个Collection View? - How can I have multiple Collection Views in one View Controller? 一个视图控制器上的3个集合视图 - 3 Collection Views on One View Controller 如何在一个视图中制作两个集合视图? - How to make two collection views in one view? 为什么 didSelect function 仅适用于视图 controller 中的两个集合视图之一? - Why didSelect function only works for one of two collection views in a view controller? 一个视图控制器中具有不同重用单元格的两个集合视图 - Two Collection Views with Different Reuse Cells in One View Controller 在集合视图控制器中快速移动两个视图 - Swift a two views within a collection view controller 如何使两个集合视图出现在同一个视图控制器上? - How to make two collection views appear on the same view controller? 如何在一个视图控制器中合并多个集合视图? - How to incorporate multiple collection views in one view controller? 如何在一个视图控制器中将xib加载到两个容器视图中 - How to load xib into two Container Views within one view controller 您可以在一个视图控制器上使用两个不同的相机按钮来显示两个不同的图像吗? (快速3) - Can you have two different camera buttons for two different image views on one view controller? (Swift 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM