2017-11-29 3 views
0

私はコレクションビューで作業しています。コレクションビューに3つの行を表示したいと思います。各行は2つのセルを持っています。問題は、私はrow.Pleaseで親切に私が試みた私のコードを参照することができません。UICollectionViewセルサイズ - swift 3/4

import UIKit 

class ProductsTableViewCell: UITableViewCell { 

@IBOutlet weak var productsCollections: UICollectionView! 
override func awakeFromNib() { 
    super.awakeFromNib() 
    productsCollections.isScrollEnabled = false 
    productsCollections.dataSource = self as? UICollectionViewDataSource 
    productsCollections.delegate = self as? UICollectionViewDelegate 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

} 

extension ProductsTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 6 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Product", for: indexPath) 
    return cell 
} 

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    return collectionView.frame.width/9 
} 

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let collectionViewWidth = collectionView.bounds.width 
    let collectionViewHeight = collectionView.bounds.height 
    return CGSize(width: collectionViewWidth/2.0 , height: collectionViewHeight/2.5) 
} 

[This is the output i got.][1]} 

I want like this.totally 6 rows.Per row 2 cells i want to display

答えて

0

まず第一に、あなたが必要な行の高さを計算します。したがって、画面の解像度に依存します。

1-可変

fileprivate var cellHeight: CGFloat 

2-)

self.cellHeight = yourContainerFrameHeight/3 

3-(のviewDidLoadで一度計算minimumLineSpacingForSectionAt

4-計算を削除宣言:例えば

セルサイズ。

私は確信していませんが、ここではCollectionViewのサイズを使用しないでください(sizeForItemAt)。代わりに、固定UIViewまたは画面サイズを使用できます。私たちはすでにそれを特定しているからです。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    flowLayout.invalidateLayout() 
    let vMiddle = self.contentView.frame.size.width/2.0 
    //this calculation for prevent next row shifts and fit rows 
    let cellWidth = 
     indexPath.row % 2 == 0 ? floor(vMiddle) : self.contentView.frame.size.width - floor(vMiddle) 
    //print("---------- cell width : \(cellWidth)") 
    return CGSize(width: cellWidth, height: self.cellHeight) 
} 
: この例では、あなたは、あなたや画面サイズ

に変更し、これをしようとする必要がありますself.contentViewあり

関連する問題