I am trying to have two collection views on one view controller. There are many posts guiding people through this but I cannot tell what I am doing wrong.
My code is the following,
class FeedVC: UIViewController {
#IBOutlet var collectionView: UICollectionView!
#IBOutlet var collectionViewProgress: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
collectionViewProgress.dataSource = self
collectionViewProgress.delegate = self
}
}
extension FeedVC: UICollectionViewDelegate, UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (collectionView == self.collectionView){
return 10
}else{
return 1
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("hre")
if (collectionView == self.collectionView){
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UnclaimedCVC", for: indexPath) as? UnclaimedCVC {
cell.configureCell()
return cell
}else{
return UICollectionViewCell()
}
}else if(collectionView == self.collectionViewProgress){
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InProgressCVC", for: indexPath) as? InProgressCVC {
cell.configureCell()
return cell
}else{
return UICollectionViewCell()
}
}else{
return UICollectionViewCell()
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
if (collectionView == self.collectionView){
return 1
}else{
return 1
}
}
}
I am currently getting the error,
'could not dequeue a view of kind: UICollectionElementKindCell with
identifier InProgressCVC - must register a nib or a class for the
identifier or connect a prototype cell in a storyboard'
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")
Related
I try to show multiple collection views in one ViewController, but I get crash with error:
Thread 1: Exception: "could not dequeue a view of kind: UICollectionElementKindCell with identifier MainCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard"
I registered all the cells in Storyboard and this is my code for ViewController:
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
#IBOutlet weak var collectionView: UICollectionView!
#IBOutlet weak var horizontalNumbers: UICollectionView!
#IBOutlet weak var verticalNumbers: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
horizontalNumbers.delegate = self
horizontalNumbers.dataSource = self
verticalNumbers.delegate = self
verticalNumbers.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == collectionView {
return 81
} else {
return 9
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == collectionView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainCell", for: indexPath) as! CollectionViewCell
cell.backgroundColor = .systemGreen
return cell
} else if collectionView == horizontalNumbers {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Horizontal", for: indexPath) as! HorizontalNumbersCell
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Vertical", for: indexPath) as! VerticalNumbersCell
return cell
}
}
}
I checked everything twice and looked for some examples of code, but don't release why I get crash.
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.
I have a UICollectionView inside a UICollectionViewCell, and a separate NSObject that is the dataSource. I am able to set the dataSource for the external UICollectionView, but not the internal one.
Here's the cell containing the internal UICollectionView:
class FeaturedCell: UICollectionViewCell, UICollectionViewDelegate {
#IBOutlet var collectionView: UICollectionView!
let data = FeaturedData()
override init(frame: CGRect) {
super.init(frame: frame)
//setUp()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func setUp() {
collectionView.dataSource = data
collectionView.delegate = self
collectionView.reloadData()
}
}
extension FeaturedCell {
func setCollectionViewDataSourceDelegate <D: FeaturedData> (_ dataSourceDelegate: D, forRow row: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.reloadData()
print("Reload Data")
}
}
And the UIView containing the external UICollectionView:
class MainView: UIView, UICollectionViewDelegate {
#IBOutlet var collectionView: UICollectionView!
let data = MainData()
override func awakeFromNib() {
setUp()
}
func setUp() {
collectionView.dataSource = data
collectionView.delegate = self
collectionView.backgroundColor = UIColor.orange
collectionView.collectionViewLayout = createLayout()
collectionView.isPagingEnabled = true
collectionView.bounces = false
collectionView.allowsSelection = true
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print("WillDisplay")
guard let cell: FeaturedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedCell", for: indexPath) as? FeaturedCell else {
fatalError("Unable to dequeue FeaturedCell.")
}
cell.setCollectionViewDataSourceDelegate(featuredData, forRow: indexPath.item)
}
}
Both of these methods are being called, but the dataSource and delegates are never being set. I also followed this tutorial exactly (with a UITableView, even) and it still would not set the dataSource or delegate. What am I doing wrong?
I think this will help
class FeaturedCell: UICollectionViewCell {
#IBOutlet var collectionView: UICollectionView!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
extension FeaturedCell {
func setCollectionViewDataSourceDelegate <T: UICollectionViewDelegate , D: FeaturedData> (delegate: T dataSource: D, forRow row: Int) {
collectionView.delegate = delegate
collectionView.dataSource = dataSource
collectionView.reloadData()
print("Reload Data")
}
}
and in the MainView
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print("WillDisplay")
guard let cell: FeaturedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedCell", for: indexPath) as? FeaturedCell else {
fatalError("Unable to dequeue FeaturedCell.")
}
cell.setCollectionViewDataSourceDelegate(self, featuredData, forRow: indexPath.item)
}
And for the tutorial you followed this a Github link for the tutorial, compare your code with that and see where did you missed out.
Hope this will helps.
Solved it. The problem was that this:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print("WillDisplay")
guard let cell: FeaturedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedCell", for: indexPath) as? FeaturedCell else {
fatalError("Unable to dequeue FeaturedCell.")
}
cell.setCollectionViewDataSourceDelegate(featuredData, forRow: indexPath.item)
}
Needed to be changed to this:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print("WillDisplay")
guard let featuredCell = cell as? FeaturedCell else { return }
featuredCell.setCollectionViewDataSourceDelegate(featuredData, forRow: indexPath.item)
}
I have build a UICollectionViewController programmatically which returns 4 cells. HomeCell, TrendingCell, SubscriptionCell and AccountCell.
All 4 cells should be different and you can scroll them horizontally. <--->.
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout{
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(HomeCell.self, forCellWithReuseIdentifier: homeCellId)
collectionView?.register(TrendingCell.self, forCellWithReuseIdentifier: trendingCellId)
collectionView?.register(SubscriptionCell.self, forCellWithReuseIdentifier: subscriptionCellId)
collectionView?.register(AccountCell.self, forCellWithReuseIdentifier: accountCellId)
}
}
Lets take the first cell of the HomeController called HomeCell to illustrate my problem. Homecell has three custom cells called VideoCell, CategoryCell and UserSearchCell.
class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
let searchId = "sarchId"
let scrollId = "scrollId"
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.dataSource = self
cv.delegate = self
return cv
}()
override func setupViews() {
super.setupViews()
.....
// register three different cells within HomeCell
collectionView.register(VideoCell.self, forCellWithReuseIdentifier: cellId)
collectionView.register(CategoryCell.self, forCellWithReuseIdentifier: scrollId)
collectionView.register(UserSearchCell.self, forCellWithReuseIdentifier: searchId) //
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
In HomeCell I register the UserSearchCell as third cell.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoId", for: indexPath)
} else if indexPath.item == 1{
cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryId, for: indexPath)
}else {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: searchId, for: indexPath)
}
return cell
}
If I click on that element my goal is to push a new ViewController to the navigationController. But I have no access and don't know how to change the view within that nested structure. I tried the didSelectItem method within HomeCell class and was able to print something the console when clicking the third cell but couldn't change the view.
class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 2 {
print(123)
}
....
}
Please help. Is there a way to change the View within the didSelectItem method of HomeCell ??
You need to write a protocol to refer back to your HomeController.
protocol HomeCellProtocol {
func pushNavigation(_ vc: UIViewController)
}
add write delegate property to HomeCell class with
class HomeCell: ..... {
var delegate: HomeCellProtocol?
}
and make HomeController confirm to HomeCellProtocol with
extention HomeController: HomeCellProtocol {
func pushNavigation(_ vc: UIViewController) {
self.navigationController?.pushViewController(vc, animated: true)
}
}
and when you setup your HomeCell you need to set your delegate in HomeController
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: homeCellId, for: indexPath) as HomeCell;
cell.delegate = self // Set the delegate
return cell
}
finally, you can call push function within HomeCell with
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 2 {
let vc = UIViewController();
self.delegate?.pushNavigation(vc);
}
}
I have a viewController there are collection and tableview and tableview cell their is another collection view . if we not use tableview code my first colletion work fine but when we use tableview .My application became crash and show error
[VGS.CustumCell collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance .
Please help I am newer in iOS. Any help would be appreciated. Thanks in advance
override func viewDidLoad() {
super.viewDidLoad()
dayArray = ["Mon","Tue","Wed","Thu","Fri","Sat"]
let identifier = "TimeViewCell"
collection.registerNib(UINib(nibName: "TimeViewCell", bundle: nil), forCellWithReuseIdentifier: identifier)
self.timetableTableview.estimatedRowHeight = 79
self.timetableTableview.rowHeight = UITableViewAutomaticDimension
self.timetableTableview.setNeedsLayout()
self.timetableTableview.layoutIfNeeded()
// Do any additional setup after loading the view.
}
extension TimeTableController : UICollectionViewDelegate {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView.tag == 0 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("TimeViewCell", forIndexPath: indexPath) as! TimeViewCell
cell.daylabel.text = dayArray[indexPath.row] as? String
if (indexPath.row == 0){
cell.backgroundColor = UIColor.firstColor()
} else if (indexPath.row == 1){
cell.backgroundColor = UIColor.secondfColor()
}else if (indexPath.row == 2){
cell.backgroundColor = UIColor.thirdColor()
}else if (indexPath.row == 3){
cell.backgroundColor = UIColor.fourthColor()
}else if (indexPath.row == 4){
cell.backgroundColor = UIColor.fifthColor()
}else if (indexPath.row == 5){
cell.backgroundColor = UIColor.sixthColor()
}
return cell
} else {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Timetable", forIndexPath: indexPath) as! Timetable
return cell
}
}
}
extension TimeTableController: UITableViewDelegate,UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "CustumCell"
var cell: CustumCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustumCell
if cell == nil {
var nib:Array = NSBundle.mainBundle().loadNibNamed("CustumCell", owner: self, options: nil)
cell = nib[7] as? CustumCell
let identifier = "Timetable"
cell.timecollectionView.registerNib(UINib(nibName: "Timetable", bundle: nil), forCellWithReuseIdentifier: identifier)
}
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
return cell
}
}
I just had a similar issue. My problem was that I hooked up the delegate and datasource both on the storyboard and in my viewDidLoad. This cancels it out. I first tried deleting my delegate & data source connections from my viewController file, but it still wasn't displaying the contents in my IBOutlets. But when I did vice versa, it did the trick. Make sure you declare it in one or the other.
My solution:
Disconnect via storyboard.
Declare datasource and delegate in your view controller's viewDidLoad.
override func viewDidLoad() {
super.viewDidLoad()
mainCategoryCollectionView.delegate = self
mainCategoryCollectionView.dataSource = self
}
You are not declaring the dataSource for the collectionView. Add it to the extension, where you are already assigning the delegate.
extension TimeTableController: UICollectionViewDelegate, UICollectionViewDataSource {
And do not forget declare the required functions:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {}
I tried many days to realise this:
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. :)
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
}
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
}
You can use the factory design pattern to build two different collection views and return them via functions. Here's my working version for swift 4.
This code goes in a separate helper file:
import UIKit
class collectionViews {
static func collectionViewOne() -> UICollectionView {
let layout = UICollectionViewFlowLayout()
let collectionViewOne = UICollectionView(frame: CGRect(x: 0, y: 20, width: 200, height: 100), collectionViewLayout: layout)
return collectionViewOne
}
static func collectionViewTwo() -> UICollectionView {
let layout = UICollectionViewFlowLayout()
let collectionViewTwo = UICollectionView(frame: CGRect(x: 0, y: 300, width: 200, height: 100), collectionViewLayout: layout)
return collectionViewTwo
}
}
And here is the view controller code:
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
let collectionViewOne = collectionViews.collectionViewOne()
let collectionViewTwo = collectionViews.collectionViewTwo()
var myArray = ["1", "2"]
var myArray2 = ["3", "4"]
override func viewDidLoad() {
super.viewDidLoad()
collectionViewOne.delegate = self
collectionViewOne.dataSource = self
collectionViewOne.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
view.addSubview(collectionViewOne)
collectionViewTwo.delegate = self
collectionViewTwo.dataSource = self
collectionViewTwo.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell2")
view.addSubview(collectionViewTwo)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.collectionViewOne {
return myArray.count
} else {
return myArray2.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.collectionViewOne {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath)
myCell.backgroundColor = UIColor.red
return myCell
} else {
let myCell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell2", for: indexPath as IndexPath)
myCell2.backgroundColor = UIColor.blue
return myCell2
}
}
}
Result
You can also name the collection views outlets differently (without subclassing):
#IBOutlet weak var collectionView: UICollectionView!
#IBOutlet weak var SecondCollectioView: UICollectionView!
method:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as UICollectionViewCell
if(collectionView == self.SecondCollectioView) {
cell.backgroundColor = UIColor.black
} else {
cell.backgroundColor = self.randomColor()
}
return cell;
}
This is will be an another way.
Here's my working version for swift 5 and Xcode 11:
create outlets for corresponding collectionviews: outlets:
#IBOutlet weak var bgCollectionView: UICollectionView!
#IBOutlet weak var frontCollectionView: UICollectionView!
var arrImages = [String : [UIImage]]()
arrImages is contain like
override func viewDidLoad() {
super.viewDidLoad()
arrImages = [
"frontImg": [//Front UIImage array],
"bgImg": [//Background UIImage array]
]
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let arrImg = arrImages["bgImg"] {
return arrImg.count
} else if let arrImg = arrImages["frontImg"]{
return arrImg.count
}
return 0
}
You can do this two ways
Using CollectionView Outlets
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
if collectionView == self.bgCollectionView{
if let arrImg = arrImages["bgImg"]{
cell.imgView.image = arrImg[indexPath.row]
}
}else{
if let arrImg = arrImages["frontImg"]{
cell.imgView.image = arrImg[indexPath.row]
}
}
return cell
}
Using CollectionView Tag:
Here Background Images collectionview tag is 1 and Front Images collectionview tag is 2.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
if collectionView == collectionView.viewWithTag(1){
if let arrImg = arrImages["bgImg"]{
cell.imgView.image = arrImg[indexPath.row]
}
}else{
if let arrImg = arrImages["frontImg"]{
cell.imgView.image = arrImg[indexPath.row]
}
}
return cell
}
Please Add Tag in CollectionView Like this:
Thank You. Hope It's working for you !!
Swift 5 Answer!
If you try connecting both collectionViews to the same view controller Xcode will throw an error "Outlets cannot connect to repeating content"
Solution:
Head to Storyboard
Connect the first collectionView via outlet, set the delegate/dataSource in viewDidLoad and then add a tag to the second collectionView by heading to the attributes inspector in storyboard and change the value from 0 to 1
Select the secondCollectionView and go to the connections inspector and select delegate and drag the connection to the UIViewController and the same for the dataSource.
Simply check which collectionView is passing through.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == collectionView.viewWithTag(1) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCollectionView", for: indexPath)
return cell
}
else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCollectionView", for: indexPath) as! HomeMainCollectionViewCell
cell.configureCell()
return cell}
}