如何设计不同的收集单元

How to design different collection cell

我有一个 collectionView 和不同的 collectionCell,我想根据单元格设置一个 scale/height/size。我该怎么做 ?

这是我的不同单元格的图像:一个 title cell 和两个不同的 cell image。一个是视图的宽度 / 3,另一个是 / 2。我不能在故事板中使用自动布局约束来做到这一点,所以我需要在代码中做。我正在尝试使用 collectionViewLayout。

根据您的要求,您可以这样做

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)
   }

}

输出:

实施

class MyViewController: UIViewController, UICollectionViewDataSource , UICollectionViewDelegate , UICollectionViewFlowLayout

ViewDidLoad

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

添加流布局委托

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
    }