在UICollectionView中显示不同数量的单元格

我想有一个UICollectionView ,在第一行有一个单元格的所有行的宽度,第二行有3个单元格。 我已经研究过文档,但是我无法做到。 像这样的东西:

在这里输入图像说明

提前致谢

在故事板中的集合视图中创build2个单元格原型,并将它们设置为重用标识符

在你的ViewController的.m文件中实现这个function

 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 6; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell; if (/*condition for small cell*/)// eg indexPath.row % 3 != 0 cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallCell" forIndexPath:indexPath]; else cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bigCell" forIndexPath:indexPath]; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (/*condition for small cell*/) // eg indexPath.row % 3 != 0 return CGSizeMake(65, 50); return CGSizeMake(318, 50); } 

做完这个之后你会得到这样的东西:

在这里输入图像说明