How to design different collection cell

323 views Asked by At

i've a collectionView with different collectionCell and i want to set a scale/height/size depending of the cell. How can i do it ?

Here is the image of my different cell : a title cell, and 2 different cell image. One is width of the view / 3 and the other is / 2. I can't do it with auto layout constraint in the story board so i need to do in the code. I'm trying to use the collectionViewLayout.

CollectionView

2

There are 2 answers

0
dahiya_boy On BEST ANSWER

implement

class MyViewController: UIViewController, UICollectionViewDataSource , UICollectionViewDelegate , UICollectionViewFlowLayout

in ViewDidLoad

collectionView.delegate = self;
collectionView.datasource = self;

add delegate of flow layout

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    if indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2{
      return CGSize(self.view.frame.size.width / 3, 100)
     }

  else{
      return CGSize(self.view.frame.size.width / 2, 100)
    }
//  return the size you what
    }
3
Himanshu Moradiya On

As per your requirement you can do like this way

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
   if (indexPath.row >= 0 && indexPath.row < 3) {
        return CGSize(width: self.view.frame.size.width / 3.15, height: 150)
        // here give size for 3 collectionviewcell in one row . i use 3.5 beacause of i put spacing 10 pix left and right side of collectionview.
   }
   else if (indexPath.row >=3 && indexPath.row < 5){
       return CGSize(width: self.view.frame.size.width / 2.20, height: 150)
        // here give size for 2 collectionviewcell in one row.
   }else{
      return CGSize(width: self.view.frame.size.width / 3.15, height: 150)
   }

}

Output :

as they requirement 6 cell in collectionview