[英]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.