简体   繁体   English

如何在swift3中的UICollectionView中显示两个不同的单元格

[英]How to display two different cells in UICollectionView in swift3

I am trying to achieve the following image by using the UICollectionViewCell. 我正在尝试通过使用UICollectionViewCell实现以下图像。 样本图片

But i have got 3 different cases for this: A) A particular type of journey only onward journey time is visible. 但是我有3种不同的情况:A)特定类型的旅程只有向前的旅程时间可见。 B) A particular type of journey both onward journey and return journey is visible(as shown in the below image). B)可以看到向前和向后的特定旅程类型(如下图所示)。 C) A particular type of journey has 2 onward journey and 2 return journey time. C)特定类型的旅程有2个继续旅程和2个返回旅程时间。

So i am trying to use the collection view for this. 所以我试图为此使用集合视图。 Am i correct in doing this approach and how to achieve this logic using collection view? 我采用这种方法正确吗?如何使用集合视图实现这种逻辑?

I would probably subclass my UICollectionViewCell, define all necessary layout type enums and pass it in my cellForItemAtIndexPath as a parameter in a custom method, so the custom collection view cell knows how to layout itself. 我可能会继承UICollectionViewCell的子类,定义所有必需的布局类型枚举,并将其作为自定义方法中的参数传递到cellForItemAtIndexPath中,以便自定义集合视图单元格知道如何进行布局。

Something like this: 像这样:

enum MyLayoutType
{
    case layoutA
    case layoutB
    case layoutC
}

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

// MARK: Collection View DataSource

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
    {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCustomCVCell", for: indexPath) as! MyCustomCVCell

        // Decide here what layout type you would like to set

        cell.setUpWithType(layoutType: .layoutA)
        return cell
    }
}

And then, in your custom collection view cell subclass (don't forget to assign the Custom class in your interface builder file): 然后,在您的定制集合视图单元格子类中(不要忘记在界面构建器文件中分配Custom类):

class MyCustomCVCell: UICollectionViewCell
{
    func setUpWithType(layoutType: MyLayoutType)
    {
        switch layoutType
        {
        case .layoutA:
            self.backgroundColor = .red
        case .layoutB:
            self.backgroundColor = .yellow
        case .layoutC:
            self.backgroundColor = .blue
        }
    }
}

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

相关问题 如何使用swift3在UITableView中的两个不同单元格中显示两个数组 - How to display two arrays in two different cells in UITableView using swift3 如何限制UICollectionView的单元格显示数量-Swift - How to limit number of cells display of UICollectionView - swift Swift3:如何在UICollectionView中获取图像? - Swift3: How to get images in UICollectionView? Swift 3中的UICollectionView在各节中显示其他单元格 - UICollectionView in Swift 3 display additional Cells in sections 如何根据Swift3中的内容自动更改UICollectionView Cell的高度? - How to change the height of UICollectionView Cell automatically according to content in Swift3? Swift:UICollectionView iOS的一行中不同数量的单元格 - Swift: Different number of cells in a row of a UICollectionView iOS 在UICollectionView中连续显示不同数量的单元格 - Display different amount of cells in a row within a UICollectionView UICollectionView显示不同的单元格取决于方向 - UICollectionView display different cells depend orientation 在UICollectionView中的每个部分显示两个单元格 - Display two cells per section in UICollectionView Swift 3如何在索引UICollectionView中显示每个项目的不同数据 - Swift 3 How to display different data for each item in index UICollectionView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM