swift 如何使用垂直滚动一次显示一个collectionView单元格

qacovj5a  于 2023-11-16  发布在  Swift
关注(0)|答案(4)|浏览(131)

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

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
    }

}

字符串


的数据

svdrlsy4

svdrlsy41#

您需要符合UICollectionViewDelegateFlowLayout,然后使用

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

字符串

yjghlzjz

yjghlzjz2#

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

字符串

wfveoks0

wfveoks03#

在创建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)

}

uxh89sit

uxh89sit4#

你要做两件事:
1.首先:
设置UICollectionViewdelegate,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: self.collectionView.frame.width,
        height: self.collectionView.frame.height
    )
}


干杯。

相关问题