Skip to content

Instantly share code, notes, and snippets.

@BradB132
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BradB132/2a5a593279fd99e55b4f to your computer and use it in GitHub Desktop.
Save BradB132/2a5a593279fd99e55b4f to your computer and use it in GitHub Desktop.
GettingStartedWithCustomUICollectionViewLayouts-3
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray* elementsInRect = [NSMutableArray array];
//iterate over all cells in this collection
for(NSUInteger i = 0; i < [self.collectionView numberOfSections]; i++)
{
for(NSUInteger j = 0; j < [self.collectionView numberOfItemsInSection:i]; j++)
{
//this is the cell at row j in section i
CGRect cellFrame = CGRectMake(/* calculate your origin x */,
/* calculate your origin y */,
/* calculate your width */,
/* calculate your height */);
//see if the collection view needs this cell
if(CGRectIntersectsRect(cellFrame, rect))
{
//create the attributes object
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:j inSection:i];
UICollectionViewLayoutAttributes* attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
//set the frame for this attributes object
attr.frame = cellFrame;
[elementsInRect addObject:attr];
}
}
}
return elementsInRect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment