How to Add Multiple Collection Views in a Uiviewcontroller in Swift

How can I add multiple collection views in a UIViewController in Swift?

This is possible, you just need to add each UICollectionView as a subview, and set the delegate and dataSource to your UIViewController.

Here's a quick example. Assuming you have one UICollectionView working, you should be able to adapt this code to your own uses to add a second fairly easily:

let collectionViewA = UICollectionView()
let collectionViewB = UICollectionView()
let collectionViewAIdentifier = "CollectionViewACell"
let collectionViewBIdentifier = "CollectionViewBCell"

override func viewDidLoad() {
// Initialize the collection views, set the desired frames
collectionViewA.delegate = self
collectionViewB.delegate = self

collectionViewA.dataSource = self
collectionViewB.dataSource = self

self.view.addSubview(collectionViewA)
self.view.addSubview(collectionViewB)
}

In the cellForItemAtIndexPath delegate function:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView == self.collectionViewA {
let cellA = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewAIdentifier) as UICollectionViewCell

// Set up cell
return cellA
}

else {
let cellB = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewBIdentifier) as UICollectionViewCell

// ...Set up cell

return cellB
}
}

In the numberOfItemsInSection function:

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

return 0 // Replace with count of your data for collectionViewB
}

Two Collection Views in One View Controller iOS

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
}
}

Multiple CollectionViews in one ViewController

You need to register cell for all three collectionView.Take the collectionView for example.

try

collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MainCell") // if it is loaded in nib, and nib name is CollectionViewCell

or

collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "MainCell") // if it is written just in code

in the viewDidLoad.

Do the same thing for the horizontalNumbers, etc.

Check https://developer.apple.com/documentation/uikit/uicollectionview/1618089-register for more details.

How to add multiple collection view in a view

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

}
}

Multiple Collection Views with in a viewController with different number of cells

As @Rajesh73 pointed out in the comment , the problem was in assigning one layout to both the collectionViews. So I gave the both collectionViews different layouts and it solved the problem.

Thus replacing

  let layout =              UICollectionViewFlowLayout()
fontCollection = UICollectionView(frame: frame, collectionViewLayout: layout)
colorCollection = UICollectionView(frame: frame, collectionViewLayout: layout)

with

  let fontsLayout =      UICollectionViewFlowLayout()
fontCollection = UICollectionView(frame: frame,collectionViewLayout: fontsLayout)
let colorsLayout = UICollectionViewFlowLayout()
colorsCollection = UICollectionView(frame: frame collectionViewLayout: colorsLayout)

solved the issue.

How can I have multiple Collection Views in one View Controller?

Here is a two-section version of the code you wrote. I'm not sure if it addresses the error you are seeing, but it's how you'd do multiple collection views on one page.

class xyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

var tableImagesOne: [String] = ["1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png"]
var tableImagesTwo: [String] = ["1.png", "2.png", "3.png", "4.png", "5.png"]

override func viewDidLoad() {
// Initialize the collection views, set the desired frames
}


func numberOfSectionsInCollectionView(_ collectionView: UICollectionView) -> Int {
return 2
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return tableImagesOne.count
}
else {
return tableImagesTwo.count
}
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let row = indexPath.row
let section = indexPath.section

let cell:xyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! xyCollectionViewCell

if section == 0 {
cell.xyImgCell.image = UIImage(named: tableImagesOne[row])
}
else if section == 1 {
cell.xyImgCell.image = UIImage(named: tableImagesTwo[row])
}
return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
println("cell \(indexPath.section) : \(indexPath.row) selected")
}

}


Related Topics



Leave a reply



Submit