2016-05-22 3 views
0

Alors, je veux régler la hauteur UICollectionViewCell du cellForItemAtIndexPath au lieu de partirComment définir la hauteur de UICollectionViewCell à partir de cellForItemAtIndexPath?

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

Comment puis-je faire? Donc, je veux donner de la hauteur et la largeur à mes cellules dans cellForItemAtIndexPath, pas dans le collectionViewLayout: sizeForItemAtIndexPath

Répondre

2

Vous devez toujours utiliser sizeForItemAtIndexPath, mais vous pouvez déclarer une variable publique sur le haut de votre fichier rapide:

var cellSize = CGSize() 

et dans l'écriture quelque chose comme délégué cellForRowAtIndexPath auparavant return cell:

cellSize = CGSizeMake(100, 100) // set your desired width and height 

alors:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return cellSize 
} 
0

Une façon vous pouvez fournir les estimer la hauteur avec UICollectionViewFLowLayout

UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*) self.CollectionViewLayout.collectionViewLayout; 
    flowLayout.estimatedItemSize = CGSizeMake(self.view.frame.size.width,yourheight); 

mais si toutes les cellules a besoin d'avoir des hauteurs différentes, alors vous devez mettre en œuvre sous forme de méthode sizeForItemAtIndexPath UICollecionViewController.

>  (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout 
> sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 
>  return CGSizeMake(width, height); 
>  
>  }