1个UIviewController中有2个UIcollectionview,具有2个不同的UIcollectionflowlayout?

时间:2018-07-11 13:23:37

标签: ios swift uicollectionview

我可能知道有什么方法可以在具有2个不同UIcollectionflow布局的1个UIviewcontroller中实现2个UIcollectionview。 主要问题是一个UIcollectionview符合iOS UIcollectionviewflowlayout,另一个则符合Waterfalllayout。因为我现在面临的问题是我不能在一个UIviewcontroller中同时拥有两个委托函数。谢谢大家。

 func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        }


func collectionView(_ collectionView: UICollectionView, layout: WaterfallLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    }

1 个答案:

答案 0 :(得分:0)

     //: Playground - noun: a place where people can play

import UIKit

class Controller: UIViewController, UICollectionViewDelegateFlowLayout, WaterfallLayoutDelegate {

    let collectionView = UICollectionView(frame: CGRect.zero)
    let waterfallCollectionView = UICollectionView(frame: CGRect.zero)

    override func viewDidLoad() {

        collectionView.delegate = self
        collectionView.collectionViewLayout = UICollectionViewFlowLayout()

        waterfallCollectionView.delegate = self
        let waterfallLayout = WaterfallLayout()
        waterfallLayout.delegate = self
        waterfallCollectionView.collectionViewLayout = waterfallLayout
    }

    //MARK: - WaterfallLayoutDelegate

    func collectionView(_ collectionView: UICollectionView, layout: WaterfallLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize.zero
    }

    //MARK: - UICollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize.zero
    }
}

//MARK: - Just for test

protocol WaterfallLayoutDelegate: class {

    func collectionView(_ collectionView: UICollectionView, layout: WaterfallLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
}

class WaterfallLayout: UICollectionViewLayout {

    weak var delegate: WaterfallLayoutDelegate?
}

一切正常。