The end result is to have 2 collection views on one view controller. Both pulling from different sources as well as one should scroll horizontally while the other vertically.
Please advise on how to achieve this programatically.
The end result is to have 2 collection views on one view controller. Both pulling from different sources as well as one should scroll horizontally while the other vertically.
Please advise on how to achieve this programatically.
I haven't used
UICollectionView
before, but as it inherits from UIScrollView, I'm taking a chance that it's pretty similar toUITableView
.When using one CollectionView, I'm assuming you have to set
collectionView.delegate = self;
andcollectionView.dataSource = self
, and in the.h
-file, make sure your class is using<UICollectionViewDelegate, UICollectionViewDataSource>
or something similar. When you're setting the delegate of the collectionView to your own view (self
), you're making sure that the data provided for the collectionView comes from your own class, in the delegate-methods. I'm sure you already know this, as that should be pretty straight forward with one single collectionView.When you are using two collectionViews, then you have to set
This will in turn do so that both collectionViews will call the delegate methods. For instance, the delegate method
-collectionView:cellForItemAtIndexPath:
will be called twice. Once for collection1, and once for collection2.To make sure they get the correct data sent to them, you should create a simple check in the beginning of every delegate and dataSource method, like this:
Here, I'm checking if
collectionView
is equal tocollection1
orcollection2
. The delegate methods are providingcollectionView
as the UICollectionView it is calling the method for, and that has to be either of the two. This can look suspicious if you have called one of your collectionViews forcollectionView
though, so be sure to name them logically.