Reputation: 517
There seems to be a similar question here but it didn't solve my problem.
I created two collectionViews in my viewController as follows :
let y = self.view.frame.height
titleView = UIView(frame : CGRect(x: 0 , y: 50 , width: self.view.frame.width, height: y - 100 ))
let middle = CGPoint(x: 10, y: titleView.bounds.height/10)
let frame = CGRect(origin: middle , size: CGSize(width: self.view.frame.width - 20 , height: 70))
let layout = UICollectionViewFlowLayout()
fontCollection = UICollectionView(frame: frame, collectionViewLayout: layout)
fontCollection.dataSource = self
fontCollection.delegate = self
fontCollection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "fontsIdentifier")
fontCollection.backgroundColor = UIColor.white
// Do any additional setup after
colorCollection = UICollectionView(frame: frame, collectionViewLayout: layout)
//colorCollection.frame.origin.y = fontCollection.frame.origin.y + (2 * fontCollection.frame.height)
colorCollection.dataSource = self
colorCollection.delegate = self
colorCollection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "colorsIdentifier")
colorCollection.backgroundColor = UIColor.white
// Do any additional setup after
These are my UICollectionViewDataSource
methods :
func numberOfSections(in collectionView: UICollectionView) -> Int {
collectionView.reloadData()
collectionView.collectionViewLayout.invalidateLayout()
return 1
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(collectionView == self.colorCollection ){
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colorsIdentifier", for: indexPath)
cell.backgroundColor = .darkGray
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fontsIdentifier", for: indexPath)
cell.backgroundColor = .gray
return cell
}
}
Then I use a segmented control
to switch between the two collectionViews
.
Everything goes as desired upto this point.However when I want to allocate different number of cells to either collectionView
I get this error :
UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 0 - 6}
This is my numberOfItemsInSection
method
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(collectionView == self.colorCollection ){ return 9 }
else if(collectionView == self.fontCollection ){ return 6 }
return 0
}
Am I doing something wrong? What am I missing?
Upvotes: 4
Views: 1526
Reputation: 517
As @Rajesh73 pointed out in the comment , the problem was in assigning one layout to both the collectionViews
. So I gave the both collectionViews different layouts and it solved the problem.
Thus replacing
let layout = UICollectionViewFlowLayout()
fontCollection = UICollectionView(frame: frame, collectionViewLayout: layout)
colorCollection = UICollectionView(frame: frame, collectionViewLayout: layout)
with
let fontsLayout = UICollectionViewFlowLayout()
fontCollection = UICollectionView(frame: frame,collectionViewLayout: fontsLayout)
let colorsLayout = UICollectionViewFlowLayout()
colorsCollection = UICollectionView(frame: frame collectionViewLayout: colorsLayout)
solved the issue.
Upvotes: 3