私は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
}
}
私はすぐにSwift 3.0を使います。あまりにも癖がありません。 – RaymondWoo