Cells order in UICollectionView

后端 未结 2 568
既然无缘
既然无缘 2020-12-11 10:30

I need to create a UICollectionView with cells of different sizes (1x1, 2x2, 2x1, 3x2.5). I\'ve already code to add cells depending on which size they are using

2条回答
  •  不知归路
    2020-12-11 11:10

    A sample example by subclassing UICollectionViewLayout. All values are hard coded, just to explicit the logic behind it. Of course, that could be optimized.

    @interface CustomCollectionViewLayout ()
    
    @property (nonatomic, strong) NSMutableDictionary *cellLayouts;
    @property (nonatomic, assign) CGSize              unitSize;
    
    @end
    
    @implementation CustomCollectionViewLayout
    
    
    -(id)initWithSize:(CGSize)size
    {
      self = [super init];
      if (self)
      {
        _unitSize = CGSizeMake(size.width/3,150);
        _cellLayouts = [[NSMutableDictionary alloc] init];
      }
      return self;
    }
    -(void)prepareLayout
    {
    
      for (NSInteger i = 0; i < [[self collectionView] numberOfItemsInSection:0]; i ++)
      {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        CGRect frame;
        switch ([indexPath item])
        {
          case 0:
            frame = CGRectMake(0, 0, _unitSize.width*3, _unitSize.height*2.5);
            break;
          case 1:
            frame = CGRectMake(0, _unitSize.height*2.5, _unitSize.width, _unitSize.height);
            break;
          case 2:
            frame = CGRectMake(_unitSize.width, _unitSize.height*2.5, _unitSize.width*2, _unitSize.height*2);
            break;
          case 3:
            frame = CGRectMake(0, _unitSize.height*2.5+_unitSize.height, _unitSize.width, _unitSize.height);
            break;
          case 4:
            frame = CGRectMake(0, _unitSize.height*2.5+_unitSize.height+_unitSize.height, _unitSize.width*2, _unitSize.height);
            break;
          case 5:
            frame = CGRectMake(_unitSize.width*2, _unitSize.height*2.5+_unitSize.height+_unitSize.height, _unitSize.width, _unitSize.height);
            break;
          case 6:
            frame = CGRectMake(0, _unitSize.height*2.5+_unitSize.height+_unitSize.height+_unitSize.height, _unitSize.width, _unitSize.height);
            break;
          case 7:
            frame = CGRectMake(_unitSize.width, _unitSize.height*2.5+_unitSize.height+_unitSize.height+_unitSize.height, _unitSize.width*2, _unitSize.height);
            break;
          case 8:
            frame = CGRectMake(0, _unitSize.height*2.5+_unitSize.height+_unitSize.height+_unitSize.height+_unitSize.height, _unitSize.width*3, _unitSize.height*2.5);
            break;
          default:
            frame = CGRectZero;
            break;
        }
        [attributes setFrame:frame];
        [[self cellLayouts] setObject:attributes forKey:indexPath];
      }
    }
    
    -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
      NSMutableArray *retAttributes = [[NSMutableArray alloc] init];
      for (NSIndexPath *anIndexPath in [self cellLayouts])
      {
        UICollectionViewLayoutAttributes *attributes = [self cellLayouts][anIndexPath];
        if (CGRectIntersectsRect(rect, [attributes frame]))
        {
          [retAttributes addObject:attributes];
        }
      }
      return retAttributes;
    }
    
    -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    
      return [self cellLayouts][indexPath];
    }
    
    -(CGSize)collectionViewContentSize
    {
      return CGSizeMake(_unitSize.width*3, _unitSize.height*9);
    }
    
    @end
    

    Then, you just have to call :

    CustomCollectionViewLayout *layout = [[CustomCollectionViewLayout alloc] initWithSize:self.myCollectionView.bounds.frame.size];
    [self.myCollectionView setCollectionViewLayout:layout];
    

    Rendering :

提交回复
热议问题