0

So as far as I know, the "protocol method":

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

Automatically sets the bounds of the cell I created. This is all fine and dandy for my of the UICollectionViewCells, but I need one of them to be in a location that I specify. Any pointers in the right direction would be appreciated.

Tulon
  • 4,011
  • 6
  • 36
  • 56
user2539621
  • 351
  • 3
  • 18

1 Answers1

1

Can I assume you are also using an instance of UICollectionViewFlowLayout as your collection view's layout object?

If so, the quick and dirty answer is to subclass UICollectionViewFlowLayout and override the -layoutAttributesForItemAtIndexPath: method:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.item == 2) // or whatever specific item you're trying to override
    {
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        layoutAttributes.frame = CGRectMake(0,0,100,100); // or whatever...
        return layoutAttributes;
    }
    else
    {
        return [super layoutAttributesForItemAtIndexPath:indexPath];
    }
}

You will probably also need to override -layoutAttributesForElementsInRect::

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *layoutAttributes = [super layoutAttributesForElementInRect:rect];
    if (CGRectContainsRect(rect, CGRectMake(0, 0, 100, 100))) // replace this CGRectMake with the custom frame of your cell...
    {
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        layoutAttributes.frame = CGRectMake(0,0,100,100); // or whatever...
        return [layoutAttributes arrayByAddingObject:layoutAttributes];
    }
    else
    {
        return layoutAttributes;
    }
}

Then use your new subclass in place of UICollectionViewFlowLayout when you create your collection view.

Josh Hinman
  • 6,745
  • 7
  • 38
  • 47
  • Alright, thanks! I was already using a subclass of UICollectionViewFlowLayout called [lxreorderablecollectionviewflowlayout](https://github.com/lxcid/LXReorderableCollectionViewFlowLayout) but I just played around with the existing code in it along with the things you provided and got it working. – user2539621 Jul 31 '13 at 01:00
  • 1
    in the layoutAttributesForElementsInRect method NSArray *layoutAttributes = [super layoutAttributesForElementInRect:rect]; should be replaced with NSArray *layoutAttributes = [super layoutAttributesForElementsInRect rect]; – jerrygdm Jan 09 '15 at 11:32
  • @JoshHinman Can you explain what layoutAttributesForElementsInRect does and why it also has to be overridden – Stefan Jun 04 '17 at 06:05
  • indexPath doesn't exist in layoutAttributesForElementsInRect – Matt Hudson Feb 27 '18 at 18:12