2017-03-17 41 views
1
刪除多餘的空間

Desired Output like this image在集合視圖中斯威夫特

代碼:

let reuseIdentifier = "cell" 
var items = ["1", "2", "3", "4","5","6","7","8", "9", "10", "11","12","13","14"] 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.items.count 
} 

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell 
    // Use the outlet in our custom class to get a reference to the UILabel in the cell 
    cell.myLabel.text = self.items[indexPath.item] 
    cell.backgroundColor = UIColor.red 
    return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: CGFloat((collectionView.frame.size.width/2) - 8), height: CGFloat(250)) 
} 
+1

圖片不可見 –

+0

可以粘貼截圖嗎? –

+0

您應該解釋您遇到的問題並顯示您嘗試解決的問題。橫軸上是否有額外的空間?垂直軸?細胞之間?部分之間?請詳細說明,以便您可以幫助我們爲您提供幫助。 – Laffen

回答

0

這根據你的要求做希望這將有助於你得到一個想法。

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
      layout.sectionInset = UIEdgeInsets(top: 10, left: 1, bottom: 5, right: 0) // according to your requirements 
      layout.minimumInteritemSpacing = 1 // according to your requirements 
      layout.minimumLineSpacing = 1// according to your requirements 

如果你想要更多的幫助,或者這不是爲你工作,你也可以通過link它可以幫助你實現你的目標。點擊這裏link

0
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
{ 
return CGSize(width: CGFloat((collectionView.frame.size.width/2) - 8), height: (collectionView.frame.size. height/3) - 8)) 
}