【问题标题】:UICollectionView cell size - swift 3/4UICollectionView 单元格大小 - 快速 3/4
【发布时间】:2017-11-29 05:23:36
【问题描述】:

我正在处理集合视图。我想在集合视图中显示 3 行。每行有两个单元格。问题是我无法将单元格放在一行中。请参考我尝试过的代码。

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

【问题讨论】:

    标签: swift collectionview


    【解决方案1】:

    首先,计算您需要的行高。所以这取决于屏幕分辨率。

    例如:

    1- 声明一个变量

    fileprivate var cellHeight: CGFloat
    

    2- 在 vi​​ewDidLoad() 中计算一次

    self.cellHeight = yourContainerFrameHeight / 3
    

    3- 删除minimumLineSpacingForSectionAt

    4- 计算像元大小。

    我不确定,但您不应该在此处使用 CollectionView 的大小(在sizeForItemAt 中)。相反,您可以使用固定的 UIView 或屏幕尺寸。因为我们已经识别了它。 这个例子有self.contentView你必须改变你的或屏幕大小

    试试这个:

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-06
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多