简体   繁体   English

我如何快速在集合视图中加载多个自定义视图

[英]How can i load multiple custom views in collection view swift

I have two collection view cells A and B , i need to load these cells simultaneously.我有两个集合视图单元格 A 和 B ,我需要同时加载这些单元格。 But i didn't found any solutions但我没有找到任何解决方案

         firstCollectionView.register(UINib(nibName: "A", bundle:  Bundle.main), forCellWithReuseIdentifier: "A")
     firstCollectionView.register(UINib(nibName: "B", bundle:  Bundle.main), forCellWithReuseIdentifier: "B")

These are the two views and how can load 2 views at time.这是两个视图以及如何一次加载两个视图。

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "A", for:indexPath) as? A

How do you want to divide the different cells type?你想如何划分不同的细胞类型? with number?与号码? Like, if raw = 0,2,4,6 etc you will have firstCell and if raw = 1,3,5 etc you will have secendCell?比如,如果 raw = 0,2,4,6 等等,你会有 firstCell,如果 raw = 1,3,5 等等你会有 secendCell?

So maybe with something like this :所以也许有这样的事情:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = UICollectionViewCell()

        cell = collectionView.register(UINib(nibName: "B", bundle:  Bundle.main), forCellWithReuseIdentifier: "B")

        if indexPath.row % 2 == 0 {
            cell = collectionView..register(UINib(nibName: "A", bundle:  Bundle.main), forCellWithReuseIdentifier: "A")
        }

        return cell
    }

Register CustomCollectionViewCell in the viewDidLoad:viewDidLoad:注册CustomCollectionViewCell viewDidLoad:

    var nib1 = UINib(nibName: "CustomCollectionViewCell1", bundle: nil)
    self.firstCollectionView().registerNib(nib1, forCellReuseIdentifier: "CustomCell1")
    var nib2 = UINib(nibName: "CustomCollectionViewCell2", bundle: nil)
    self.firstCollectionView().registerNib(nib2, forCellReuseIdentifier: "CustomCell2")

Now return your cell here cellForItemAtIndexPath: method,现在在这里返回您的单元格cellForItemAtIndexPath:方法,

//As per your condition check cell index or section or any other your condition.


 if indexPath.row % 2 == 0 {  

      // Create an instance of CustomCollectionViewCell1
     var cell: CustomCollectionViewCell1? = tableView.dequeueReusableCell(withIdentifier: "CustomCell1")
        if self.cell == nil {
            self.cell = CustomCollectionViewCell1(style: .subtitle, reuseIdentifier: "CustomCell1")
        }
     return cell!

    }else{
      // Create an instance of CustomCollectionViewCell2
     var cell: CustomCollectionViewCell2? = tableView.dequeueReusableCell(withIdentifier: "CustomCell2")
        if self.cell == nil {
            self.cell = CustomCollectionViewCell2(style: .subtitle, reuseIdentifier: "CustomCell2")
        }
     return cell!
    }

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

相关问题 如何在 Swift 中的一个 UIViewController 中添加多个集合视图? - How can I add multiple collection views in a UIViewController in Swift? 如何在一个View Controller中拥有多个Collection View? - How can I have multiple Collection Views in one View Controller? 如何使用Swift中的应用内购买功能从多个视图中删除广告(已在1个视图中使用) - How can I remove ads from multiple views using In-App purchase in Swift (Already works in 1 view) 如何在Swift,XCode中为Collection View设置自定义水平滚动? - How can I set custom horizontal scroll for Collection View in Swift, XCode? 如何解决集合视图闪烁问题? (迅速) - How Can I Solve the Collection View Blinking Issue? (Swift) Swift我如何为jsqmessageViewcontroller的集合制作自定义单元格 - Swift How can i make custom cell for the collection of jsqmessageViewcontroller Swift:我可以在集合/表格视图单元格内加载网址数据吗? - Swift: Can I load Url data inside a collection/table view cell? 如何在 pdf 视图上移动/拖动多个视图? - How I can move/drag multiple views on pdf view? 如何在Swift中的多个视图控制器中重用表视图? - How can I reuse a table view in multiple view controllers in Swift? 如何在Swift 3中使用动画加载/删除自定义视图 - How to load/remove Custom View with animation in Swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM