【问题标题】:How to show collectionView cell one at a time with vertical scrolling如何使用垂直滚动一次显示一个collectionView单元格
【发布时间】:2019-11-19 18:13:08
【问题描述】:

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

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
    }

}

【问题讨论】:

  • 能否请您包含一些代码?
  • numberOfItemsInSection 内将return self.lableArray.count 更改为return 1
  • @Kamran 我想显示所有单元格,但一次显示一个单元格,而不仅仅是一个单元格。
  • @RanuDhurandhar 然后您需要将单元格的高度设置为与以下答案中建议的 collectionView 相同。如果您需要按上述方式创建视图,则可能需要实现自定义视图。

标签: swift layout uicollectionviewcell


【解决方案1】:

你需要符合UICollectionViewDelegateFlowLayout然后使用

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

【讨论】:

    【解决方案2】:

    UICollectionViewFlowLayout 具有委托方法sizeForItemAt,可用于为您正在显示的UICollectionViewCell 提供CGSize。你只需要顺从就可以了!

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多