Multiple Uicollectionview in One Controller

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.

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

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
}

Multiple UICollectionView in one controller

The problem was that I was using the same layout object for each collection. In retrospect that makes sense, but you have to make sure you create different layouts for each collectionView.

Multiple UICollectionViews in one ViewController

Use one collectionview(which scrolls vertically) inside your view controller.
Check out how to implement the following delegate method

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView

This delegate method can be used to have a custom view at the top ( header view for collection view)
Inside that header view you can have your second collectionview(the one which scrolls horizontally)

I Have implemented the same thing in this app

https://itunes.apple.com/in/app/baetter-poll-on-the-go/id1134956263?mt=8

Two CollectionViews on one View Controller

In viewDidLoad put

collectionView.register(UnclaimedCVC.self, forCellWithReuseIdentifier: "UnclaimedCVC ")

collectionViewProgress.register(InProgressCVC.self, forCellWithReuseIdentifier: "InProgressCVC ")

If you created them as Xibs

 collectionView.register(UINib.init(nibName: "UnclaimedCVC", bundle: nil), forCellWithReuseIdentifier: "UnclaimedCVC")

collectionViewProgress.register(UINib.init(nibName: "InProgressCVC", bundle: nil), forCellWithReuseIdentifier: "InProgressCVC")

How to use two CollectionView in one ViewController?

What I think is a clearer method is to use a second collectionViewController and a custom container view controller.

That way, the differentiation in the delegate methods is no longer needed.



Related Topics



Leave a reply



Submit