2016-12-24 25 views
0

私はswift 3.0でデモを書き、UICollectionViewをカスタムビューでラップしました。 CollcionViewの遅延読み込みと現在のビューをデータソースとして設定しますが、カスタム表示拡張ではUICollectionViewDataSource直接エラーに準拠しています。それに対処する方法?カスタムビューがプロトコルUICollectionViewDataSourceに準拠していません

コード:// MARK

//セルID

fileprivate let ContentCellID = "ContentCellID" 

class PageContentView: UIView { 

    // MARK:- lazy attributes 
    fileprivate lazy var collecionView : UICollectionView = { 
     // 1.create layout 
     let layout = UICollectionViewFlowLayout() 
     layout.itemSize = self.bounds.size 
     layout.minimumLineSpacing = 0 
     layout.minimumInteritemSpacing = 0 
     layout.scrollDirection = .horizontal 
     // 2.create UICollectionView 
     let collecionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout) 
     collecionView.showsHorizontalScrollIndicator = false 
     collecionView.isPagingEnabled = true 
     collecionView.bounces = false 
     collecionView.dataSource = self 
     // register cell 
     collecionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: ContentCellID) 
     return collecionView 
    }() 
    // MARK:- define attributes 
    fileprivate var childVcs : [UIViewController] 
    fileprivate var parentVc : UIViewController 
    // MARK:- custom constructor 
    init(frame: CGRect, childVcs : [UIViewController], parentVc : UIViewController) { 
     self.childVcs = childVcs 
     self.parentVc = parentVc 
     super.init(frame: frame) 
     setupUI() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

: - セットアップUI

extension PageContentView { 
    fileprivate func setupUI() { 

     for childVc in childVcs { 
      parentVc.addChildViewController(childVc) 
     } 

     addSubview(collecionView) 
     collecionView.frame = bounds 
    } 
} 

// MARK: - UICollectionViewDataSource

extension PageContentView : UICollectionViewDataSource { 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return childVcs.count 
    } 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collecionView.dequeueReusableCell(withReuseIdentifier: ContentCellID, for: indexPath) 

     let childVc = childVcs[indexPath.row] 
     childVc.view.frame = cell.contentView.bounds 
     cell.contentView.addSubview(childVc.view) 
     return cell 
    } 
} 

enter image description here

答えて

2

あなたはそれを実装し、必要な方法collectionView(_:numberOfItemsInSection:)を実装するのを忘れていました。

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

注:デフォルトのセクションが1であるよりも実装なので、そのメソッドを削除し、私は上記追加したcollectionView(_:numberOfItemsInSection:)childVcs.countを返さない場合あなたが実装されている方法numberOfSections(in:)は、任意の方法です。

+1

私はすぐにSwift 3.0を使います。あまりにも癖がありません。 – RaymondWoo

0

あなたのビューがあると述べたプロトコルで必要なすべての機能を実装することにより、「UICollectionViewDataSource」プロトコルに準拠してください:

// required functions 
extension ThisView: UICollectionViewDataSource { 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     return UICollectionViewCell() 
    } 
} 
+0

'numberOfSections(in:)'はオプションのメソッドではありませんが、実装していない場合はデフォルトではあなたのcollectionViewセクションが1つ含まれています。 –

-1

私も上記の必要なメソッドと、この問題がありました。彼らが列挙された順番が私の問題を解決しました。上記を行い、問題が解決しない場合は、メソッドの順序を変更してください。

関連する問題