UICollectionView Objective C中的2个不同的单元大小

我想使用UICollectionView,可以创build以下布局:

像那样的东西...

正如你所看到的,这个单元有两种不同的尺寸。 一个是行的1/4,另一个是3/4。 有没有可能用UICollectionView创build这种布局?

任何人都可以教我怎么做? 或者是否有任何样品? 我已经学习了很多教程和参考。 但是还是不知道该怎么做

谢谢!

那么我已经硬编码的项目宽度暂时(72.0和23.0)。 5.0的其余部分将用于临时空间和edgeInsets。 这段代码会给你正是你想要的。

 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 10.0; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10.0; } #pragma mark - CollectionViewFlowLayout Methods - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGSize newSize = CGSizeZero; newSize.height = 100; CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGSize screenSize = screenBounds.size; if(indexPath.item % 4 == 0 || indexPath.item % 4 == 3) { //Size : 3/4th of screen newSize.width = screenSize.width * 0.23; } else { //Size : 1/4th of screen newSize.width = screenSize.width * 0.72; } return newSize; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(10, 2.0, 10, 2.0); } 
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ return [(NSString*)[arrayOfStats objectAtIndex:indexPath.row] sizeWithAttributes:NULL]; } 

使用这种方法你可以返回不同大小的单元格。 请参阅此链接取决于标签宽度的UICollectionView的dynamic单元格宽度

有不同的方法…

  1. 你可以通过实现- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath方法。

  2. 您可以按照以下教程中的描述完全自定义集合视图。

    http://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest

  3. 从你的destianation布局来看,甚至可以通过使用UITableviewCell(在单元格中加载1/4大小和3/4大小的单元格)以及通过自动布局来改变它们的大小。