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

时间:2020-04-19 05:30:10

标签: ios swift uitableview uicollectionview uicollectionviewcell

如何从表视图单元格以及集合视图单元格图像内部创建具有不同单元格大小的此类集合视图,需要从网址单元格How to create like this collection view with different cell size inside table view下载

我尝试了下面的代码,但最后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)
    }

1 个答案:

答案 0 :(得分:0)

要在表格视图中创建集合视图,请转到此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)
}