如何在表格视图单元格内创建集合视图

Posted

技术标签:

【中文标题】如何在表格视图单元格内创建集合视图【英文标题】:How to create collection view inside table view cell 【发布时间】:2020-04-19 05:30:10 【问题描述】:

如何在表格视图单元格内创建像这样具有不同单元格大小的集合视图,以及在集合视图单元格内的图像需要从 url 单元格 下载

我尝试了下面的代码,但最后 2 个小单元格上升了,并且当从 url 集合视图下载的图像直到手动触摸集合视图才加载时

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
        let cVWidth = collectionView.frame.width
        let biggerCellWidth = (cVWidth / 2.5) - 5
        if indexPath.row == 2 || indexPath.row == 3 
            let testValue:CGFloat = 3
            return CGSize(width: (biggerCellWidth / 2) - testValue  , height: (biggerCellWidth / 2) - testValue )
        
        return CGSize(width: biggerCellWidth, height: biggerCellWidth)
    

【问题讨论】:

你应该先尝试一下。你可以在网上找到很多文章 我添加了尝试过的代码, 但最后 2 个小单元的单元位置不正确 【参考方案1】:

要在表格视图中创建集合视图,请访问link

UICollectionViewDelegateFlowLayout中有一个方法

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

您可以在其中设置集合视图单元格大小的大小。假设您要将第三个和第四个单元格的高度设置为集合视图的一半,大小为正方形。提出这个条件

if indexPath.row == 2 || indexPath.row == 3

    let height = collectionView.bounds.size.height / 2
    return CGSize(width: height, height: height)

【讨论】:

以上是关于如何在表格视图单元格内创建集合视图的主要内容,如果未能解决你的问题,请参考以下文章

在表格视图中的所有单元格都已加载后,重新加载表格视图单元格内的集合视图

在表格视图单元格内设置集合视图约束的正确方法

如何在可扩展的表格视图单元中制作动态集合视图

tableview 单元格内的集合视图重新加载数据

当在同一个单元格内单击按钮时,如何在自定义表格视图单元格内隐藏/显示特定视图

如何在表格视图单元格内的视图顶部和底部放置阴影?