Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I tried many days to realise this: enter image description here

I want to add in my UIViewController two different CollectionView. For example I want to put images in these collectionView Each CollectionView use its own images. Is this possible?

I will be very happy if somebody can give me a hand. :)

like image 909
Masterfego Avatar asked Feb 26 '15 18:02

Masterfego


People also ask

How do I have more than one type of cell in a collection view?

Alternatively, you can drag more than one cell from the Object Library into your collection view, assign a subclass and reuse identifier for the cell and design it as needed. Just like you would for a single cell.

How do I register a collection view in Swift?

Step 1: Create an Xcode project & setup CollectionView: We will use the same project and CollectionView in this tutorial. You can make a different project and CollectionView as I describe above. Step 2: Setup CollectionView cell: Create a new Cocoa Touch file. Create a class subclassing “UICollectionViewCell”.

What is collection view in Swift?

CollectionView is an object that presents the ordered collection of data items in the customizable layouts. It shows the data in the form of a grid layout. A collectionview is an instance of the UICollectionView class, which inherits the UIScrollView, which will be covered later in this tutorial.


2 Answers

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 } 
like image 129
Sam Avatar answered Sep 18 '22 13:09

Sam


Yes--this is entirely possible. You can either assign their respective UICollectionViewDelegates/UICollectionViewDataSources to different classes or subclass the CollectionViews, assigning both the delegate and data source to your current viewController and downcast your reference to collectionView in the delegation methods like so:

@IBOutlet collectionViewA: CustomCollectionViewA! @IBOutlet collectionViewB: CustomCollectionViewB!   func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {      if let a = collectionView as? CustomCollectionViewA {         return a.dequeueReusableCellWithIdentifier("reuseIdentifierA", forIndexPath: indexPath)     } else {         return collectionView.dequeueReusableCellWithIdentifier("reuseIdentifierB", forIndexPath: indexPath)         } } 

Subclass UICollectionView like this:

class CustomCollectionViewA: UICollectionView {     // add more subclass code as needed }  class CustomCollectionViewB: UICollectionView {     // add more subclass code as needed } 
like image 34
kellanburket Avatar answered Sep 16 '22 13:09

kellanburket