【问题标题】: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 个小单元的单元位置不正确

标签: ios swift uitableview uicollectionview uicollectionviewcell


【解决方案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)
}

【讨论】: