ios - 如何在一个 View 中添加多个 Collection View

标签 ios swift uicollectionview uicollectionviewcell

我正在尝试将 2 个 Collection View 合并到同一个 View 中。我实现了一切,并注意到在第二个 Collection View 中加载的数据与第一个 Collection View 相同。

这是我正在尝试的代码

    class DashBoardMainSection1CollectionViewCellClass: UICollectionViewCell
{

    @IBOutlet weak var DashBoardMainSection1CollectionViewImageListingViewOutlet    : UIImageView!

    @IBOutlet weak var DashBoardMainSection1CollectionViewLabelOutlet               : UILabel!

}

class DashBoardMainSection2CollectionViewCellClass: UICollectionViewCell
{

    @IBOutlet weak var DashBoardMainSection2CollectionViewImageListingViewOutlet    : UIImageView!

    @IBOutlet weak var DashBoardMainSection2CollectionViewLabelOutlet               : UILabel!

}


private let reuseIdentifierDashBoardMainSection1Cell = "dashBoardMainSection1CollectionViewCellIdentifier"

private let reuseIdentifierDashBoardMainSection2Cell = "dashBoardMainSection2CollectionViewCellIdentifier"

class DashBoardViewControllerMain: UIViewController,  UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{



    @IBOutlet weak var DashBoardMainSection1CollectionViewOutlet    :   UICollectionView!

    @IBOutlet weak var DashBoardMainSection2CollectionViewOutlet    :   UICollectionView!


    override func viewDidLoad()
    {
        super.viewDidLoad()



        DashBoardMainSection1CollectionViewOutlet.delegate = self;
        DashBoardMainSection2CollectionViewOutlet.delegate = self;
        DashBoardMainSection1CollectionViewOutlet.dataSource = self;
        DashBoardMainSection2CollectionViewOutlet.dataSource = self;

        // Do any additional setup after loading the view.
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        var count:Int?
        if (self.DashBoardMainSection1CollectionViewOutlet) != nil{
            count = 10
        }
        //if (self.DashBoardMainSection2CollectionViewOutlet) != nil{
        else{
            count = 5
        }

        return count!
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if let collectionView = self.DashBoardMainSection1CollectionViewOutlet{

            let cell:DashBoardMainSection1CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection1Cell, for: indexPath) as! DashBoardMainSection1CollectionViewCellClass


            cell.DashBoardMainSection1CollectionViewImageListingViewOutlet.image = UIImage(named:"Apple.png")

            cell.DashBoardMainSection1CollectionViewLabelOutlet.text = "FIRST"

            return cell



        }
        else{       
          let cell2:DashBoardMainSection2CollectionViewCellClass = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierDashBoardMainSection2Cell, for: indexPath) as! DashBoardMainSection2CollectionViewCellClass


            cell2.DashBoardMainSection2CollectionViewImageListingViewOutlet.image = UIImage(named:"Logo-Disabled.png")

            cell2.DashBoardMainSection2CollectionViewLabelOutlet.text = "SECOND"

            return cell2

        }
    }

当我运行该项目时,两个 Collection View 都显示 Apple 图片,并且单元格数为 10。

谁能帮我解决这个问题...

最佳答案

请用标签管理两个collectionview。在 viewDidLoad 方法或 Storyboard 中为 collectionview 提供标签。现在根据标签创建条件。

示例:-

DashBoardMainSection1CollectionViewOutlet.tag = 1
DashBoardMainSection2CollectionViewOutlet.tag = 2

if collectionView.tag == 1 {
   return 10
} else {
   return 5
}

关于ios - 如何在一个 View 中添加多个 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45433704/

相关文章:

swift - 如何向自定义 UICollectionViewCell NIB 添加手势?

ios - 在没有模拟器的情况下运行 iOS 代码

ios - ios中uicollectionview单元格的谷歌分析事件

ios - Google Maps SDK didChangeCameraPosition() 被重复调用

swift - Var getter和setter的困惑

ios - UICollectionView 显示存储在文档目录中的图像

ios - 如何让 react-native 使用 Mac OS X El Capitan 中的主机文件?

ios - Firebase快速上传多张图片

ios - 单元格宽度对于 tableview 宽度来说太大了

ios - CollectionView performBatchUpdates 崩溃 iOS 7