2017-08-28 25 views
0

here is some snap of it我試圖用這個代碼,但它不工作:我想表明在一排uicollectionview兩種細胞中的所有iPhone機型

我collecionview細胞:

尺寸爲:166:32 最小間距:10:10個

部分插圖:20:20

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt 
     indexPath: IndexPath) -> CGSize 
    { 
     if collectionView == MainCollectionView 
     { 
      let padding : CGFloat = 50 
      let collectionViewSize = MainCollectionView.frame.size.width - padding 

      return CGSize(width: collectionViewSize/2, height: collectionViewSize/2) 

     } 
     return CGSize(width: 0, height: 0) 
    } 
+0

您可以在一個部分使用兩個單元格。 –

回答

0

使用UICollectionViewDelegateFlowLayout在你看來 並調用委託方法

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let width = self.view.frame.width 
    return CGSize(width: (width)/2, height: (width)/2) // width & height are the same to make a square cell 
} 
+0

它不工作是否有其他方式? – user8198079

+0

你有沒有設置採集代理自我 – AbhiRaz

0
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{ 

    let width = (collectionView.frame.width/2)-0.5 
    return CGSize(width:width , height: width) 

} 
+0

這不是工作.. – user8198079

+0

在哪個方向滾動 –

+0

方向滾動是垂直的 – user8198079

0

嘗試實施這些UICollectionViewDelegateFlowLayoutdelegate methods,以達到預期的效果:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
{ 
    return CGSize(width: (collectionView.bounds.width - 10 - 20*2)/2, height: collectionView.bounds.height) 
    //In collectionView.bounds.width - 10 - 20*2 : 
    //10 for cell spacing and 
    //20*2 for section insets on left and right 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat 
{ 
    return 10 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets 
{ 
    return UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) 
} 

更加清晰地明確你的要求,這樣我可以進一步幫助您。

+0

它不工作是否有其他方式? – user8198079

+0

什麼不起作用?顯示你正在獲得的輸出以及你想要達到的目標。 – PGDev

+0

我想在uicolectionview中排列兩個單元格。 – user8198079

0

你的代碼是完美的,但你使用的委託方法可能沒有被調用。根據您使用的swift語法版本,嘗試使用最新的委託方法。並且確保你已經通過使自己的收藏觀察了該代表的觀察者。

+0

我只有5秒的問題和它的手機。根據我,我使用更新的語法,如果你有任何建議,請幫助我。 – user8198079

+0

問題是特定於設備還是iOS版本? –