我怎样才能让这两个 UICollectionViews 出现在同一个视图中?

How can I make both of these UICollectionViews appear in the same View?

我在一个视图中需要两个 UICollectionViews

他们有两个不同的 CollectionViewCell classes。第一个 CollectionView 单元格 class 是 ConfirmOffersCollectionViewCell,第二个是 ConfirmRequestsCollectionViewCell.

如果我将两个单元格设置为相同的 class,我可以让 CollectionViews 出现并运行,但它们显示相同的图像。我需要它们由两个单独的图像数组填充。

我唯一知道要做的就是复制并粘贴我的 func collectionView() 代码并更改 cellForItemAtIndexPathnumberOfItemsInSection 属性。但是我无法 运行 相同的功能两次。

代码如下:

@IBOutlet weak var collectionView1: UICollectionView!

@IBOutlet weak var collectionView2: UICollectionView!




func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return images1.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: ConfirmOffersCollectionViewCell = collectionView1.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ConfirmOffersCollectionViewCell
    images1[indexPath.row].getDataInBackgroundWithBlock{
        (imageData: NSData?, error: NSError?) -> Void in
        if error == nil {
            let image = UIImage(data: imageData!)
            cell.image.image = image
        }
    }
    return cell
}




func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return images2.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: ConfirmRequestsCollectionViewCell = collectionView2.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ConfirmRequestsCollectionViewCell
    images2[indexPath.row].getDataInBackgroundWithBlock{
        (imageData: NSData?, error: NSError?) -> Void in
        if error == nil {
            let image = UIImage(data: imageData!)
            cell.image.image = image
        }
    }
    return cell
}

您不能在单个 class 中使用相同的函数。相反,您需要以某种方式识别每个集合视图(通过创建出口或给它们一个标签),然后在每个函数中检查 collectionview 参数并将其与您的集合视图进行比较。示例:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView = secondCollectionView{
       return images2.count
    }else{
       return images1.count
    }

}

此外,您还需要在 cellForRowAtIndexPath

进行类似检查