Методы источника данных UICollectionView не вызываются - PullRequest
0 голосов
/ 29 ноября 2018

Я выполняю рефакторинг своего ViewControllers, и один из них содержит collectionView, но теперь источник данных больше не вызывается.

Мой 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 голосов
/ 29 ноября 2018
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
}()
...