2016-12-24 40 views
0

我用swift 3.0寫了一個演示,並用自定義視圖包裝了UICollectionView。延遲加載collecionView並將當前視圖設置爲數據源,但在自定義視圖擴展中會遵循UICollectionViewDataSource直接錯誤。如何處理它?自定義視圖不符合協議UICollectionViewDataSource

代碼:

// CELLID

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") 
    } 
} 

// MARK: - 設置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 
} 

注:您已實現的方法numberOfSections(in:)是可選的方法,如果你不小於默認段實施爲1,所以去除方法,並返回childVcs.countcollectionView(_:numberOfItemsInSection:),我已經在上面添加。

+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

我有這個問題,即使有上面​​列出所需的方法。他們列出的順序解決了我的問題。因此,如果您仍然遇到問題,請重新排序方法。