【问题标题】: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)
}

【讨论】:

  • 您使用的是自定义单元格还是情节提要内的单元格?
  • 在情节提要中使用单元格类
  • 单元格的大小是多少?
  • 161 x 115(宽 x 高)
  • 已更新答案,请立即尝试。另外,你有没有像这样确认过委托prntscr.com/tqh7z8
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多