2016-12-02 18 views
0

我正在嘗試在集合視圖中爲單元格大小更改設置動畫。UICollectionViewCell在sizeForItemAt中不可用

在 'didSelectItemAt':

  collectionView.performBatchUpdates({ 
       collectionView.collectionViewLayout.invalidateLayout() 
       let cell = collectionView.cellForItem(at: indexPath) 
       let frame = cell?.frame 
       cell?.frame = CGRect(x: (frame?.origin.x)!, y: (frame?.origin.y)!, width: (frame?.size.width)! + 100, height: (frame?.size.height)!) 
      }, completion: nil) 

這應該設置單元格的大小,但沒有任何反應。再次單擊單元格時,我發現單元格的大小保持爲+ 100(忽略每次都會增長的事實 - 只是試圖立即使用它)。

在sizeForItemAt函數中,任何嘗試檢索單元格都返回nil。

collectionView.numberOfItems(inSection: indexPath.section) 

回報3,但

collectionView.cellForItem(at: indexPath) 

返回nil。爲什麼?

我怎樣才能得到該單元格的大小爲ItemForItemAt範圍內給定的索引路徑: ???

回答

0

顯然sizeForItemAt在cellForItem之前被調用。

0

確定indexPath.row < 3?因爲如果不是那麼它將返回零。在cellForItem中放置一個斷點,看看會發生什麼。

+0

是的,它肯定是0.我遇到的問題是,在第一個.reloadData()已經運行並且所有代表被調用之前,我正在查看該代碼的執行情況。換句話說,該表尚未填充數據。 –

相關問題