I have an UICollectionView
with 30 items of UICollectionViewCell
inside. There are 8 cells which are currently visible to the screen, the rest are pretty much invisible. I'm able to get the frame value from all of those visible cells.
My question is what should I do to get frame value from all cells, including the invisible one.
This is my code.
// some code before
for (int i = 0; i < [collectionView numberOfItemsInSection:0]; i++) {
cell = [collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
if (!cell) {
[collectionView performBatchUpdates:^{
[collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
[collectionView reloadData];
} completion:^(BOOL finished) {
cell = [collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
NSLog(@"- after batch update %d %@", i, NSStringFromCGRect(cell.frame));
}];
}
NSLog(@"- %d %@", i, NSStringFromCGRect(cell.frame));
}
// another long code
The results was, I'm able to get right frame of first 8 cells. The next cells (the one which are invisible) have wrong frame.
This is the log:
2013-07-08 16:38:57.904 My App[71245:c07] - 0 {{2, 2}, {217, 326}}
2013-07-08 16:38:57.905 My App[71245:c07] - 1 {{231, 2}, {217, 326}}
2013-07-08 16:38:57.906 My App[71245:c07] - 2 {{460, 2}, {217, 326}}
2013-07-08 16:38:57.906 My App[71245:c07] - 3 {{689, 2}, {217, 315}}
2013-07-08 16:38:57.907 My App[71245:c07] - 4 {{689, 340}, {217, 326}}
2013-07-08 16:38:57.909 My App[71245:c07] - 5 {{2, 340}, {217, 329}}
2013-07-08 16:38:57.909 My App[71245:c07] - 6 {{231, 340}, {217, 315}}
2013-07-08 16:38:57.910 My App[71245:c07] - 7 {{460, 340}, {217, 286}}
2013-07-08 16:38:57.910 My App[71245:c07] - 8 {{0, 0}, {0, 0}}
2013-07-08 16:38:57.911 My App[71245:c07] - 9 {{0, 0}, {0, 0}}
2013-07-08 16:38:57.911 My App[71245:c07] - 10 {{0, 0}, {0, 0}}
2013-07-08 16:38:57.912 My App[71245:c07] - 11 {{0, 0}, {0, 0}}
2013-07-08 16:38:57.912 My App[71245:c07] - 12 {{0, 0}, {0, 0}}
2013-07-08 16:38:57.913 My App[71245:c07] - 13 {{0, 0}, {0, 0}}
2013-07-08 16:38:57.913 My App[71245:c07] - 14 {{0, 0}, {0, 0}}
2013-07-08 16:38:57.904 My App[71245:c07] - after batch update 8 {{689, 640}, {217, 326}}
2013-07-08 16:38:57.905 My App[71245:c07] - after batch update 9 {{689, 640}, {217, 326}}
2013-07-08 16:38:57.906 My App[71245:c07] - after batch update 10 {{2, 640}, {217, 326}}
2013-07-08 16:38:57.906 My App[71245:c07] - after batch update 11 {{231, 640}, {217, 315}}
2013-07-08 16:38:57.907 My App[71245:c07] - after batch update 12 {{460, 920}, {217, 326}}
2013-07-08 16:38:57.909 My App[71245:c07] - after batch update 13 {{2, 920}, {217, 329}}
2013-07-08 16:38:57.909 My App[71245:c07] - after batch update 14 {{231, 920}, {217, 315}}
You don't need to make cells visible to get their frame, the collection view manages the frames (and other layout attributes) in its collectionViewLayout
, so you can get the frames from that.
NSInteger numberOfCells = [self.collectionView numberOfItemsInSection:0];
UICollectionViewLayout *layout = self.collectionView.collectionViewLayout;
for (NSInteger i = 0; i < numberOfCells; i++) {
UICollectionViewLayoutAttributes *layoutAttributes = [layout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
CGRect cellFrame = layoutAttributes.frame;
NSLog(@"%i - %@", i, NSStringFromCGRect(cellFrame));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With