如何在集合视图中为不同的行实现不同数量的列

时间:2017-04-21 11:17:38

标签: ios swift swift3 uicollectionview flowlayout

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

enter image description here

我试图通过使用以下链接中的瀑布流布局来实现它,但却没有取得成功。 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列相同的大小。

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

我建议在每个collectionViewCell中创建一个新的tableView,并设置tableView&#39的numberOfRows作为你想要的列数。