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
How to Write Output of Augraph to a File
Uploads Using Backgroundsessionconfiguration and Nsurlsessionuploadtask Cause App to Crash
Swift Error: Editor Placeholder in Source File
iOS Simulator Is Not Launching
Swift String Escaping When Serializing to JSON Using Codable
Swift 3 - Alamofilre 4.0 Multipart Image Upload with Progress
iOS Unrecognized Selector Sent to Instance in Swift
Visually Modifying a Uitoolbar from Xcode Storyboard
Drawing with Cgpath to Svg Output
Build Video from Uiimage Using Swift
Clip Image to Square in Swiftui
Perform Segue from App Delegate Swift
Getting Random "Facebookerrdomain Error 10000"
Share Datas Between Two Apps with iOS 8 App Groups (Using Nsuserdefaults)
Will Push Notification Still Work After Ownership Transfer in Itunesconnect
Dyld: Library Not Loaded for a Framework Within a Framework
How to Add Text, Shape and Signature in Photo Markup with Pencil Kit