2017-09-17 118 views

回答

2
  • 排除一個集合視圖

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
         // to exclude one collection view 
         if collectionView == collectionViewOne {}// first 
        if collectionView.tag == number(you set before){}// second 
    
        // to get the original cell size without changing anything 
        //let yourSize = collectionViewLayout.itemSize 
        //return collectionViewLayout.itemSize // if you have set it globally, 
        /* 
        But you can not get it, because itemSize is a property of UICollectionViewFlowLayout. 
    
        And you can't cast collectionViewLayout to UICollectionViewFlowLayout Class which is kind of UICollectionViewLayout Class . 
    
        Thats not how inheritance works. 
        */  
    } 
    
  • 得到原始細胞大小不做改變

    let indexPath = IndexPath(item: number(you choose), section: number(you choose, 0 or 1)) 
    let layout = UICollectionViewFlowLayout() // you set 
    // got the size 
    // more generally, if you have set layout per item through delegate. 
    let size = self.collectionView(collectionViewOne, layout: layout, sizeForItemAtIndexPath: indexPath) 
    // or try this ,if you have configured globally 
    let itemSize = layout.itemSize 
    
+0

我不知道爲什麼,但我無法訪問layout.itemSize,它表示「使用未解析的標識符」佈局「。」 –

+0

解答已更新。 – dengApro

+0

謝謝你,這個作品完美! –

1

假設您使用故事板,您可以將每個收藏視圖作爲視圖控制器的出口連接。例如

@IBOutlet weak var collectionViewOne: UICollectionView! 
@IBOutlet weak var collectionViewTwo: UICollectionView! 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    if collectionView == collectionViewOne { 
    // Do nothing, and set default size. 
    return layout.itemSize 
    } else { 
    //Set size here along with other setup. 
    return CGSize(width: 150, height: 150) 
    } 
}