如何制作两行水平集合视图?

Agu*_*ana 6 ios uicollectionview swift

我想在水平方向上制作两行集合视图,如下所示

在此输入图像描述

我是iOS开发的新手,一些教程只是举例说明如何垂直制作集合视图的一定数量的列,但我需要限制集合视图的行数

我尝试将节数设为两个,但行数仍然为一

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource{

    @IBOutlet var collectionView: UICollectionView!


    let imageArray = [UIImage(named: "image1"),UIImage(named: "image2"),UIImage(named: "image3"),UIImage(named: "image4"),UIImage(named: "image5"),UIImage(named: "image6"),UIImage(named: "image8"),UIImage(named: "image9")]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell
        cell.avatarView.image = imageArray[indexPath.item]
        return cell
    }


}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小智 1

请更新插入部分

func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }
Run Code Online (Sandbox Code Playgroud)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell
    cell.avatarView.image = imageArray[indexPath.item]
    return cell
}
Run Code Online (Sandbox Code Playgroud)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell
    cell.avatarView.image = imageArray[indexPath.item]
    return cell
}
Run Code Online (Sandbox Code Playgroud)