Reputation: 177
I'm using the sizeForItemAtIndexPath
function in a view controller that has two collection views.
How can I exclude one collection view in sizeForItemAtIndexPath
function, or how could I get the original cell size without changing anything?
Upvotes: 0
Views: 1269
Reputation: 4008
to exclude one collection view
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// to exclude one collection view
if collectionView == collectionViewOne {}// first
if collectionView.tag == number(you set before){}// second
// to get the original cell size without changing anything
//let yourSize = collectionViewLayout.itemSize
//return collectionViewLayout.itemSize // if you have set it globally,
/*
But you can not get it, because itemSize is a property of UICollectionViewFlowLayout.
And you can't cast collectionViewLayout to UICollectionViewFlowLayout Class which is kind of UICollectionViewLayout Class .
Thats not how inheritance works.
*/
}
to get the original cell size without changing anything
let indexPath = IndexPath(item: number(you choose), section: number(you choose, 0 or 1))
let layout = UICollectionViewFlowLayout() // you set
// got the size
// more generally, if you have set layout per item through delegate.
let size = self.collectionView(collectionViewOne, layout: layout, sizeForItemAtIndexPath: indexPath)
// or try this ,if you have configured globally
let itemSize = layout.itemSize
Upvotes: 2
Reputation: 9925
Assuming you're using storyboards, you can connect each collection view as an outlet to your view controller. E.g
@IBOutlet weak var collectionViewOne: UICollectionView!
@IBOutlet weak var collectionViewTwo: UICollectionView!
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == collectionViewOne {
// Do nothing, and set default size.
return layout.itemSize
} else {
//Set size here along with other setup.
return CGSize(width: 150, height: 150)
}
}
Upvotes: 1