2017-11-29 19 views
0

컬렉션보기에서 작업 중입니다. 컬렉션보기에서 3 행을 표시하려고합니다. 각 행에 두 개의 셀이 있습니다. 문제는 내가 행렬에 셀을 넣을 수 없다는 것입니다. 제발 제가 시도한 코드를 친절하게 참조하십시오.UICollectionView 셀 크기 - 신속 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있다