ios - 从所有(可见和不可见)UICollectionViewCell 获取框架

标签 ios objective-c uicollectionview

我有一个 UICollectionView,里面有 30 个 UICollectionViewCell 项。目前屏幕上有 8 个单元格可见,其余单元格几乎不可见。我能够从所有这些可见单元格中获取帧值。

我的问题是我应该怎么做才能从所有单元格(包括不可见的单元格)中获取帧值。

这是我的代码。

// 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

结果是,我能够获得前 8 个单元格的正确帧。下一个单元格(不可见的单元格)有错误的框架。

这是日志:

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}}

最佳答案

您不需要使单元格可见来获取它们的框架, Collection View 在其 collectionViewLayout 中管理框架(和其他布局属性),因此您可以从中获取框架。

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));
}

关于ios - 从所有(可见和不可见)UICollectionViewCell 获取框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17523861/

相关文章:

ios - 创建水平自定义布局 CollectionView

ios - 为什么 UICollectionView 在 Xcode 11 (ios 13) 中省略 Line/Intrim 间距?

ios - xcode beta 3 swift 和 FBLoginViewDelegate

ios - Swift 3 游戏音乐播放

ios - Swift - 具有零高度约束的 UIView - 错误

iphone - 在 OpenGLES 中将图像分割成小四边形网格

iOS Swift - 改变背景颜色

ios - 有没有办法断点所有按钮的 touchUpInside 方法?

ios - Objective-C -NSMutablearray 特定索引存储在另一个数组中

swift - 如何在 Swift 中使用 switch 语句实现 collectionView 功能?