我希望集合视图每行显示 2 列,我该怎么做?[请使用 Swift 5 个答案] [英] I want the Collection view to show 2 Columns per Row, how do I do that? [with Swift 5 answers please]

查看:20
本文介绍了我希望集合视图每行显示 2 列,我该怎么做?[请使用 Swift 5 个答案]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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.

This is my collection view code where I need to place the code to edit the number of columns it is 6 now.

推荐答案

不需要检查设备大小,因为我们可以使用 collectionView 宽度来计算单元格的宽度.使用单元格宽度,您可以根据需要计算高度.

You need not check the device size because we can use the collectionView width to calculate the width of the cell. Using the cell width you can calculate the height as per your need.

还有一件事:你需要使用 UICollectionViewDelegateFlowLayout &确认代表 &下面实现方法

One more thing: You need to use UICollectionViewDelegateFlowLayout & confirm the delegate & implement method below

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

这篇关于我希望集合视图每行显示 2 列,我该怎么做?[请使用 Swift 5 个答案]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆