【发布时间】: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