Avba
Avba

Reputation: 15266

How to change UICollectionViewLayoutAttributes of one cell

I would like to change the UICollectionViewLayoutAttributes attribute of one UICollectionViewCell without reloading or changing the layout object

I did

UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:indexPath];

attributes.transform = CGAffineTransformMakeScale(1.5, 1.5);

But this isn't propagating to the cell - how can this be done?

Upvotes: 2

Views: 2209

Answers (1)

Adlai Holler
Adlai Holler

Reputation: 840

-[UICollectionView layoutAttributesFor...] methods return copies of the layout attributes. You can't modify the originals after the layout returns them.

Your best bet is to subclass the layout, and whenever you return the attributes for that item (either in layoutAttributesForItem or layoutAttributesForElementsInRect), return the modified attributes.

If the change is really really temporary, you can always manipulate the cell transform manually. The collection view doesn't really care -- it may reapply the attributes when the cell needs to be reloaded, though.

Upvotes: 1

Related Questions