【问题标题】:how to make two rows horizontal collection view?如何制作两行水平集合视图?
【发布时间】:2018-09-30 01:26:58
【问题描述】:

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

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

我尝试将节数设为二,但行数仍为一

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
    }


}

【问题讨论】:

  • 集合视图可以是水平的或垂直的。你的是水平的。所以它完全地像你读过的两列垂直集合视图一样工作。在讨论如何使单元格大小合适时,只需将“宽度”替换为“高度”,就可以了。这是微不足道的。不需要部分或其他任何内容。

标签: ios swift uicollectionview


【解决方案1】:

在您的代码中添加以下内容

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

        return CGSize(width: collectionView.frame.height/2, height: collectionView.frame.height/2)

    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

【讨论】:

    【解决方案2】:

    Please update the section inset

    func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 
        }
        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
    }
    
     public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
            let padding: CGFloat =  60
            let collectionViewSize = collectionView.frame.size.height - padding
    
            return CGSize(width: 100, height: collectionViewSize/2)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 2020-01-16
      • 2019-07-23
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      相关资源
      最近更新 更多