UICollectionView数据源方法未调用

时间:2018-11-29 09:58:09

标签: swift uicollectionview datasource

我正在重构我的ViewControllers,其中一个包含collectionView,但是现在不再调用DataSource。

我的ViewController:

class CoinPageVC: UIViewController, DependencyInjectionVC, Storyboarded {

    lazy var mainView: CoinPageV = {
        let v = CoinPageV()
        v.collectionView.delegate = self
        return v
    }()

    var coin: Coin!
    var selectedBase: String!
    var viewContainer: [UIView]!

    var collectionViewViewDataSource: CollectionViewCoinPageDatasource?


    func injectDependencys(dependency: CoinPageDependency) {
        self.coin = dependency.coin
        self.selectedBase = dependency.base
        self.viewContainer = dependency.views
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionViewViewDataSource = CollectionViewCoinPageDatasource(data: viewContainer)
        self.mainView.collectionView.dataSource = self.collectionViewViewDataSource
        self.mainView.collectionView.reloadData()
    }
}
extension CoinPageVC: SetMainView {}

extension CoinPageVC: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

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

        let width:CGFloat = collectionView.bounds.width
        let height:CGFloat = collectionView.bounds.height
        // - (tabBarHeight + menuBar.frame.height + heightNavigationBarTop)
        let output = Utility.shared.CGSizeMake(width, height)
        return output
    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        let index = Int(targetContentOffset.pointee.x / view.frame.width)
        let indexPath = IndexPath(item: index, section: 0)
        mainView.menuBar.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)
    }
}

我的数据源类:

class CollectionViewCoinPageDatasource: NSObject, UICollectionViewDataSource {

    let data: [UIView]

    init(data: [UIView]){
        self.data = data
    }


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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //let outputCell: UICollectionViewCell
        let row = indexPath.item
        let outputCell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifier.coinPageCollectionViewOverviewCell.rawValue, for:indexPath) as! CollectionViewCellView
        outputCell.view = data[row]
        return outputCell
    }
}

我的收藏夹视图设置:

lazy var  collectionView: UICollectionView = {

    let layout = UICollectionViewFlowLayout()
    let frame = CGRect(x: 0, y: 0, width: 0, height: 0)
    let cv = UICollectionView(frame: frame, collectionViewLayout: layout)
    cv.register(CollectionViewCellView.self, forCellWithReuseIdentifier: Identifier.coinPageCollectionViewOverviewCell.rawValue)
    if let flowLayout = cv.collectionViewLayout as? UICollectionViewFlowLayout {
        flowLayout.scrollDirection = .horizontal
        flowLayout.minimumLineSpacing = 0
    }
    cv.backgroundColor = .green
    cv.isPagingEnabled = true
    //cv.backgroundColor = .blue

    return cv
}()

我想念什么? 我设置了数据源,还将其连接到collectionView的数据源,但是方法没有被调用。

1 个答案:

答案 0 :(得分:0)

lazy var  collectionView: UICollectionView = {

    let layout = UICollectionViewFlowLayout()
    let frame = CGRect(x: 0, y: 0, width: 0, height: 0)
    let cv = UICollectionView(frame: frame, collectionViewLayout: layout)
    cv.register(CollectionViewCellView.self, forCellWithReuseIdentifier: Identifier.coinPageCollectionViewOverviewCell.rawValue)
    if let flowLayout = cv.collectionViewLayout as? UICollectionViewFlowLayout {
        flowLayout.scrollDirection = .horizontal
        flowLayout.minimumLineSpacing = 0
    }
    cv.backgroundColor = .green
    cv.isPagingEnabled = true
    //cv.backgroundColor = .blue



    cv.delegate = self
    return cv
}()