1

將UICollectionViewController的集合視圖中的前3個單元格的位置推到導航堆棧上之前,獲取其位置的最佳方法是什麼?如何在屏幕出現之前獲取UICollectionView中的單元格框架?

例如:

let collectionViewController = SomeCollectionViewControllerSubclass(data: someData) 
// @ TODO: get the frames of the first three cells here so I can create a fancy transition 
self.navigationController.pushViewController(collectionViewController, animated: true) 

這是我已經嘗試:

let collectionViewController = SomeCollectionViewControllerSubclass(data: someData) 

// force load the view 
let view = collectionViewController.view 

// try to get a cell directly (result: nil) 
println(collectionViewController.collectionView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0) 

// try to ask the layout for the cell attributes (result: CGRectZero) 
println(collectionViewController.collectionView.collectionViewLayout.initialLayoutAttributesForAppearingItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))) 

self.navigationController.pushViewController(collectionViewController, animated: true) 

回答

2

你將不得不如果你能計算出它自己。

您不能在屏幕上訪問單元格視圖,因爲它們將在系統需要顯示它們時創建,以避免在不需要時創建它們。因此,你必須依靠你的單元格佈局知識來了解你是否可以評估它們的位置(例如,你的單元格總是有相同的高度)。

+0

是的,這是我在過渡期間一直在做的事情。我希望有一個更靈活的解決方案,但這可能要做。謝謝! – user2393462435

相關問題