2017-08-14 377 views
-1

如何調整CollectionView自定義單元格的大小,以及此代碼使用的是什麼,請大家幫忙,因爲我是iOS新手。 在此先感謝。在Swift中調整CollectionView自定義單元格大小的代表

+0

的CollectionView:UICollectionView,佈局collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath。用這種方法,而且按照這個[縮放鏈接](https://stackoverflow.com/questions/40974973 /如何調整收集視圖細胞根據設備的屏幕大小) –

+0

我已經使用它,但我的控制不會去那個方法..... –

+0

檢查無論你是否設置了collectionView委託。 –

回答

1

您需要確認協議稱爲UICollectionViewDelegateFlowLayout

// EXTENSION FOR Controller class 
extension Controller:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{ 


    // NUMBER OF SECTION IN TABLE 
    public func numberOfSections(in collectionView: UICollectionView) -> Int{ 
     return 1 
    } 

    // NUMBER OF ROWS IN PARENT SECTION 
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ 
     return Array.count 

    } 
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: 

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 

     let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ControllerCell 
     Cell.menuHeader.text = Array[indexPath.row] 
     return parentCell 
    } 

    // SIZE FOR COLLECTION VIEW CELL 
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{ 
      return CGSize(width: view.frame.width, height: 150) 
     } 
+0

是的,現在它正在使用該方法並更改單元格的大小。非常感謝。 –

+0

@VivekTyagi沒問題 –

相關問題