Reputation: 61
So, I want to set UICollectionViewCell height from the cellForItemAtIndexPath
instead of from
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
How can I do it? So I want to give height and width to my cells in cellForItemAtIndexPath
, not in the collectionViewLayout: sizeForItemAtIndexPath
Upvotes: 0
Views: 492
Reputation: 5712
One way you can provide them estimate height with UICollectionViewFLowLayout
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*) self.CollectionViewLayout.collectionViewLayout;
flowLayout.estimatedItemSize = CGSizeMake(self.view.frame.size.width,yourheight);
but if all the cell needs to have different heights then you need to implement sizeForItemAtIndexPath method form UICollecionViewController.
> (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
> sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
> return CGSizeMake(width, height);
>
> }
Upvotes: 0
Reputation: 2649
You have to still use sizeForItemAtIndexPath
, but you may declare a public variable on the top of your swift file:
var cellSize = CGSize()
and in the cellForRowAtIndexPath
delegate write something like this before return cell
:
cellSize = CGSizeMake(100, 100) // set your desired width and height
then:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return cellSize
}
Upvotes: 2