【问题标题】:I want the Collection view to show 2 Columns per Row, how do I do that? [with Swift 5 answers please]我希望集合视图每行显示 2 列,我该怎么做? [请用 Swift 5 回答]
【发布时间】:2020-07-29 10:51:25
【问题描述】:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! SkrapCollectionViewCell
cell.myLabel.text = self.items[indexPath.item]
return cell
}
这是我的集合视图代码,我需要在其中放置代码以编辑它的列数
现在是 6 岁。
【问题讨论】:
标签:
ios
swift
uicollectionview
【解决方案1】:
您不需要检查设备大小,因为我们可以使用 collectionView 的宽度来计算单元格的宽度。使用单元格宽度,您可以根据需要计算高度。
还有一件事:你需要使用 UICollectionViewDelegateFlowLayout & 确认下面的委托 & 实现方法
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let padding: CGFloat = 25
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/2, height: 115)
}