如何通过垂直滚动一次显示一个collectionView单元格

问题描述 投票:0回答:4

我想在垂直滑动时在集合视图中一次显示一个单元格。但它显示了下一个视图的一半。如何一次为一个单元格设置?

class CollectionViewCell : UICollectionViewCell {


    @IBOutlet weak var lable: UILabel!
    @IBOutlet weak var imageView: UIImageView!

}

class ViewController: UIViewController, UICollectionViewDataSource {

    let reuseIdentifier = "collectionViewCellId"
    var lableArray = ["1","2","3","4","5","6","7","8","9","10"]
    @IBOutlet weak var collectionView: UICollectionView!

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
        let item = lableArray[indexPath.row]
        cell.imageView.backgroundColor = UIColor.randomColor()
        cell.lable.text = item
        return cell
    }

}

swift layout uicollectionviewcell
4个回答
3
投票

您需要符合

UICollectionViewDelegateFlowLayout
然后使用

internal func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: view.frame.height)
}

0
投票

UICollectionViewFlowLayout
具有委托方法
sizeForItemAt
,可用于为您正在显示的
CGSize
提供
UICollectionViewCell
。您只需要遵守即可开始!

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
     //....

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.size //In case if your collection is of full size
     }
}

0
投票

创建 UICollectionView 时,添加

FlowLayout

    let layout = UICollectionViewFlowLayout.init()
    layout.scrollDirection = UICollectionView.ScrollDirection.vertical
    layout.itemSize = CGSize(width: collectView_WIDTH, height: collectView_HEIGHT)
    let collectionViewItem = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())
    collectionViewItem.setCollectionViewLayout(layout, animated: true)

添加

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

    return CGSize(width: collectView_WIDTH, height: collectView_HEIGHT)

}

0
投票

你必须做两件事:

  1. 首先:

设置 UICollectionView delegate、dataSource 并设置 isPagingEnabled = true

override func viewDidLoad() {
    super.viewDidLoad()
    
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.isPagingEnabled = true

   // rest of your work
    
}
  1. 第二:

您必须符合 UICollectionViewDelegateFlowLayout 并返回相同的宽度和高度或您的 collectionView

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: yourCollectionView.frame.width, height: yourCollectionView.frame.height)
}

干杯..

© www.soinside.com 2019 - 2024. All rights reserved.