如何在 UICollectionViewDataSource 和 UICollectionViewDelegateFlowLayout 方法中调用正确的数据源?

How to call the correct datasource within the UICollectionViewDataSource and UICollectionViewDelegateFlowLayout methods?

在具有静态 UITableViewCells 的 UITableViewController 中,我正在尝试创建一个可重复使用的网格,其中每行有一定数量的单元格,宽度和高度可变。

中,有人建议 UICollectionViews 是更好的方法。

根据https://robkerr.com/how-to-create-a-static-uicollectionview/,使用静态 UICollectionViewCells,我已经能够设置一个集合视图网格。

但是,我很难在另一个 UITableViewCell 中添加第二个独立的不同网格。

我想我可以为 UICollectionView 创建单独的 IB 出口,然后用它来 select 要使用的数据。

@IBOutlet weak var myCollectionView: UICollectionView!

@IBOutlet weak var endingsCV: UICollectionView!

extension NounsTVC: UICollectionViewDataSource
{
func collectionView( _ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    if self == myCollectionView
    {
        print("myCollectionView", cellIds.count)
        return cellIds.count}
    else if self == endingsCV
    {
        print("endingsCV", cellIds2.count)
        return cellIds2.count
    }
}

这会在找不到集合视图重用标识符的情况下崩溃。

我试过使用 UICollectionView 身份恢复 ID,但我可能没有正确的语法或位置。

如何调用正确的数据源来配置 UICollectionViewDataSource 和 UICollectionViewDelegateFlowLayout 方法中的单元格?

我不确定这是否是执行此操作的最佳方法,但如果我使用集合视图的实例化名称而不是 'self'。

@IBOutlet weak var myCollectionView: UICollectionView!

@IBOutlet weak var endingsCV: UICollectionView!

extension NounsTVC: UICollectionViewDataSource
{
func collectionView( _ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if myCollectionView == myCollectionView
{
    print("myCollectionView", cellIds.count)
    return cellIds.count}
else if endingsCV == endingsCV
{
    print("endingsCV", cellIds2.count)
    return cellIds2.count
} }