Jobins John
Jobins John

Reputation: 1278

How to add multiple collection view in a view

I am trying to incorporate 2 collection views into same view. I implemented everything and noticed that the data that is loading in the second collection view is the same as the first collection view.

Here is the code that i am trying out

    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

        }
    }

When i run the project both the collection views are showing Apple picture and the cell count is 10.

Can someone help me to solve this issue...

Upvotes: 2

Views: 2666

Answers (4)

Krunal
Krunal

Reputation: 79776

Because your condition if (self.DashBoardMainSection1CollectionViewOutlet) != nil, will always return true and code for if block will only execute everytime.

Try this:

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

        return count
    }


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

       if (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

        }
}

Upvotes: 0

Phyber
Phyber

Reputation: 1368

The problem is in the if statement:

It should be like this:

if collectionView == self.DashBoardMainSection1CollectionViewOutlet{
    //code
}

With this code, you check if the collectionView passed by the parameter is the first collectionView.

With your original code, you were making new constant named collectionView and assigning it with your first collectionView.

Upvotes: 1

Shah Nilay
Shah Nilay

Reputation: 798

Please manage both collectionview with tag. Give tag to both collectionview on viewDidLoad method or from storyboard. Now make conditions based on tag.

Example :-

DashBoardMainSection1CollectionViewOutlet.tag = 1
DashBoardMainSection2CollectionViewOutlet.tag = 2

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

Upvotes: 1

Maxime
Maxime

Reputation: 1392

If you get same data for both UICollectionView it means you are using the same DataSource. Define a separate DataSource for each UICollectionView And you should do the same for the Delegate except if you need to implement same actions for both.

Upvotes: 0

Related Questions