AzeTech
AzeTech

Reputation: 697

Two Collection Views in One View Controller iOS

I have two collection Views in One View Controller, First Collection View have class CollectionCellA with imageA as UIImageView! and labelA as UILabel!. Similarly Second Collection View with class CollectionCellB with imageB and labelB. I tried to run with following swift code but it show just blank(white) screen.

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
   var imageArroy = [UIImage(named: "1"), UIImage(named: "2"), UIImage(named: "3"), UIImage(named: "5"), UIImage(named: "6")]
   var imageArroyB = [UIImage(named: "a"), UIImage(named: "b"), UIImage(named: "c"), UIImage(named: "d"), UIImage(named: "e")]
   var labelA: ["Electronics", "Cars", "Pets", "Mobiles", "Food"]
   var labelB: ["UK", "Ireland", "India", "Germany", "Other EU"]

@IBOutlet weak var CollectionViewA: UICollectionView!
@IBOutlet weak var CollectionViewB: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    CollectionViewA.delegate = self
    CollectionViewB.delegate = self
    CollectionViewA.dataSource = self
    CollectionViewB.dataSource = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.CollectionViewA {
        return 0 // Replace with count of your data for collectionViewA
    }
    return 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == self.CollectionViewA {
        let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellA", for: indexPath) as! CollectionCellA

        // Set up cell
        return cellA
    }
    else {
         let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellB", for: indexPath) as! CollectionCellB

        // ...Set up cell
        return cellB
    }
}

Upvotes: 1

Views: 9083

Answers (3)

Tony Franzis
Tony Franzis

Reputation: 489

Add two collection view and connect delegate and data source with view countroller,create collection view cell and connect with cell and outlets

@IBOutlet weak var collectionView2: UICollectionView!
@IBOutlet weak var collectionview1: UICollectionView!
var days = ["Sun","Mon","Tues","Wed","Thur","Frid","Sat"]
var dayTask = [String]()

var task = [["Sun1","Mon1","Tues1","Wed1","Thur1","Frid1","Sat1"],["Sun2","Mon2","Tue2","Wed2"],["Sun3","Mon3","Tues3","Wed3","Thur3","Frid3","Sat3"],["Sun4","Mon4"],["Sun5","Mon5","Tues5","Wed5","Thur5","Frid5","Sat5"],["Sun6","Mon6","Tues6","Wed6"],["Sun7","Mon7","Tues7","Wed7","Thur7"]]
override func viewDidLoad() {
    super.viewDidLoad()
    dayTask = task[0]
    // Do any additional setup after loading the view.
}

Collection view data source and delgate

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == collectionview1{
        return days.count
    }else{
       return dayTask.count
    }

}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell1", for: indexPath) as! CollectionViewCell1

    if collectionView == collectionview1{
        cell.label.text = days[indexPath.row]
        let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
        collectionViewLayout?.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 40)
        collectionViewLayout?.invalidateLayout()
    }else
    {
        let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
        collectionViewLayout?.sectionInset = UIEdgeInsets(top: 10, left: 20, bottom: 0, right: 40)
        collectionViewLayout?.invalidateLayout()
        cell.label.text = dayTask[indexPath.row]
    }
    return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView == collectionview1{
        dayTask = task[indexPath.row]
        collectionView2.reloadData()
    }
}

Upvotes: 1

AzeTech
AzeTech

Reputation: 697

This code works for Two Collection View in One View Controller with images and label.

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.CollectionViewA {
            return imageArroy.count
        }

        return imageArroyB.count 

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.CollectionViewA {
            let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellA", for: indexPath) as! CollectionCellA
            cellA.imageA.image = imageArroyB[indexPath.row]
            cellA.labelA.text = labelA[indexPath.row]
            // Set up cell
            return cellA
        }

        else {
             let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellB", for: indexPath) as! CollectionCellB
            cellB.imageB.image = imageArroyB[indexPath.row]
            cellB.labelB.text = labelB[indexPath.row]
            // ...Set up cell

            return cellB
        }
}

Upvotes: 2

Vinodh
Vinodh

Reputation: 5268

For me below code works like a charm . you should send/assign value in cellForItemAt indexPath

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.CollectionViewA {
            return imageArroy.count
        }

        return imageArroyB.count

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.CollectionViewA {
            let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellA", for: indexPath) as! CollectionCellA
            // Set up cell
            cellA.lbl.text = labelA[indexPath.row]

            return cellA
        }

        else {
            let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellB", for: indexPath) as! CollectionCellB
            // ...Set up cell
            cellB.lbl.text = labelB[indexPath.row]

            return cellB
        }
    }
}


class CollectionCellA : UICollectionViewCell {
    @IBOutlet weak var lbl: UILabel!

}

class CollectionCellB : UICollectionViewCell {
    @IBOutlet weak var lbl: UILabel!

}

Upvotes: 1

Related Questions