2013-07-08 64 views
3

我有一個UICollectionView與30個項目UICollectionViewCell裏面。有8個單元目前可以在屏幕上看到,其餘幾乎看不見。我能夠從所有這些可見單元格中獲取幀值。從所有(可見和不可見)獲取框架UICollectionViewCell

我的問題是我該怎麼做才能從所有單元格中獲取幀值,包括隱形單元格。

這是我的代碼。

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

回答

11

您不需要使單元格可見以獲取其框架,集合視圖管理其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)); 
} 
+0

感謝它的工作 – nvl

0

UICollection視圖不會存儲爲細胞您的收藏中的每個元素。僅限於可見的。當你滾動時,只需重新使用那些已經不可見的。所以你不能獲得未知單元的屬性。

在MVC中,您只需要使用顯示部分數據的數據和可見單元格。所以你試圖做的是在概念上是錯誤的。

+0

是的,我知道,這就是爲什麼我用'[的CollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:我切入口:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];'向下滾動獲取不可見項。但我仍然無法得到正確的值 – nvl

+0

Tala說的是,你永遠不會有30個UICollectionViewCell實例,因爲只有在UICollectionView內部顯示的數量(可能還有幾個)。所以如果你試圖獲得所有這些,你會得到幾倍於同一個單元格的實例。 – Raspu

+0

我編輯了我的問題 – nvl

1

當我需要捕獲一個集合視圖的圖像,其單元格擴展到可見矩形之外時,我正面臨類似的問題。我不僅需要所有單元格的佈局信息,我需要系統實際繪製整個集合視圖。我嘗試使用scrollToItemAtIndexPath方法以編程方式掃視視圖,以在捕獲圖像之前強制繪製整個視圖,但沒有運氣。

解決方案是在圖像捕獲方法中設置集合視圖框架以匹配整個內容大小,並調用invalidate layout,這迫使視圖調用整個視圖中所有內容的佈局屬性。我所拍攝的圖像後,我恢復了集合視圖到原來UI的尺寸,它完美地工作:

- (UIImage *)getImageOfEntireCollectionView { 

    // Store original CV frame and get content size 
    CGPoint refCVOrigin = self.collectionView.frame.origin; 
    CGSize refCVSize = self.collectionView.frame.size; 
    CGSize contentSize = [self.collectionView.collectionViewLayout collectionViewContentSize]; 

    // expand CV size to match content size and redraw it 
    self.collectionView.frame = CGRectMake(0, 0, contentSize.width, contentSize.height); 
    [self.layout invalidateLayout]; 
    [self.collectionView reloadData]; 

    // capture image of CV 
    UIGraphicsBeginImageContext(self.collectionView.bounds.size); 
    [self.collectionView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // restore original CV frame 
    self.collectionView.frame = CGRectMake(refCVOrigin.x, refCVOrigin.y, refCVSize.width, refCVSize.height); 

    return screenshot; 
}