我如何创build像这个图像中的集合视图

在这里输入图像说明

我需要第一个两个细胞小的第二个两个大的再两个小的两个大的。 我已经尝试了下面的code.but它不给我适当的output.else任何第三方库,这是很容易实现这个等待这个问题的答案。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ if (collectionView == _CollectionSale) { if (small && j != 2){ j++; if(j==2){ small = false; k = 0; } HomeSaleCC *objHomeSaleCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC" forIndexPath:indexPath]; return objHomeSaleCC; }else if(!small && k !=2){ k++; if(k==2){ small = true; j = 0; } HomeSaleCC2 *objHomeSaleCC2=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC2" forIndexPath:indexPath]; return objHomeSaleCC2; } HomeSaleCC *objHomeSaleCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC" forIndexPath:indexPath]; return objHomeSaleCC; } else{ HomeTabCC *objHomeTabCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTabCC" forIndexPath:indexPath]; [objHomeTabCC.btnSale setTitle:[arrTitle objectAtIndex:indexPath.row] forState:UIControlStateNormal]; [objHomeTabCC.btnSale setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected]; if (indexPath.row == i) { objHomeTabCC.viewLine.hidden =NO; objHomeTabCC.btnSale.selected =YES; } else{ objHomeTabCC.viewLine.hidden =YES; objHomeTabCC.btnSale.selected =NO; } return objHomeTabCC; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ if (collectionView == _CollectionSale) { if (small && j != 2){ j++; if(j==2){ small = false; k = 0; } return CGSizeMake(153, 186); } else if(!small && k !=2){ k++; if(k==2){ small = true; j = 0; } return CGSizeMake(260, 186); }else{ return CGSizeMake(260, 186); } } else{ return CGSizeMake(260, 186); } } 

支持@ beyowulf的答案我为这个问题写了一些代码,你可以在这里find CollectionViewTest部分:

 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 7; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallCell" forIndexPath:indexPath]; [cell.contentView setBackgroundColor:[UIColor redColor]]; if (indexPath.row%4 < 2) { [cell.contentView setBackgroundColor:[UIColor greenColor]]; } return cell; } -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row % 4 < 2) { return CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 15.0f, 80.0f); } return CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 15.0f, 200.0f); } 

它看起来像这样:

在这里输入图像说明

我不确定代码的其他部分在做什么,但是如果你只想要两个小的单元格然后两个大的单元格,你可以这样做:

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.item % 4 < 2) { return CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing, 186); } return CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing, 186*2); } 

虽然我会build议返回