如何在集合视图中为不同的行实现不同的列数[重复]

Posted

技术标签:

【中文标题】如何在集合视图中为不同的行实现不同的列数[重复]【英文标题】:How to achieve different number of columns for different rows in collection view [duplicate] 【发布时间】:2017-04-21 11:17:38 【问题描述】:

如何使用集合视图实现图像中显示的布局?

我正在尝试通过使用以下链接中的瀑布流布局来实现它,但没有成功。 https://github.com/chiahsien/CHTCollectionViewWaterfallLayout

下面是我尝试这样做的代码。

override func viewDidLoad() 
    super.viewDidLoad()
   let identifier = String(describing: collectionCell.self)
    collectionView.register("collectionCell", forCellWithReuseIdentifier: "collectionCell")
    collectionView.register(UINib(nibName: "collectionCell", bundle: nil), forCellWithReuseIdentifier: identifier)
    let layout = CHTCollectionViewWaterfallLayout()
    layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    layout.minimumInteritemSpacing = 0
    layout.minimumColumnSpacing = 10
    collectionView.collectionViewLayout = layout

//用于设置单元格的大小

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize 

    let width = Double((self.collectionView.bounds.size.width-(4*minimumSpaceBetweenCells))/3)

    if indexPath.row == 0 
        return CGSize(width:width * 2, height: width * 2)
    
    else
    
        return CGSize(width:width, height: width)
    


但它只是为所有行显示具有相同大小的 2 列。

如果有人知道怎么做,请告诉我。

提前致谢。

【问题讨论】:

我认为您必须对布局进行编码。看,您在互联网上找到的大多数教程,分别具有相同的项目高度或宽度和可变宽度或高度。在您的情况下,您有 2 种具有不同高度和宽度的项目。您必须自己布置这些项目。 ***.com/questions/43186246/… ?只需计算自己的帧数,这很容易。 谢谢@Larme 我已经从你提供的链接创建了 swift 版本,它对我有用。 【参考方案1】:

我建议在每个 collectionViewCell 中创建一个新的 tableView,并将 tableView 的 numberOfRows 设置为您想要的列数。

【讨论】:

以上是关于如何在集合视图中为不同的行实现不同的列数[重复]的主要内容,如果未能解决你的问题,请参考以下文章

矩阵乘法在numpy/matlab/数学上的不同

SQL如何查询出某一列中不同值出现的次数?

Matlab:当每个单元格具有不同的行数和列数时,将二进制文件读入单元格

从表中选择不同的记录并执行重复行的列总和(托盘、总和)。并显示重复的行一次[关闭]

MS Access 2010 中的列数?

如何计算文本在 JTextArea 中的行数(以及每行中的列数)?