2017-04-05 88 views
0

我使用視圖和執行的CollectionView進去無法指定類型的NSObject的價值 - ()類名鍵入uicollectionviewdelegate

當我試圖引用的數據源和委託給我的colelctionview它給錯誤

我ClassFile的

class MenuBar : UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{ 

let collectionView : UICollectionView = { 

    let layout = UICollectionViewFlowLayout() 
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    cv.backgroundColor = UIColor.rgb(red: 230, green: 32, blue: 31) 
    cv.delegate = self // Error here 

    return cv 

}() 

override init(frame: CGRect){ 
    super.init(frame: frame) 

} 


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

}

我不能給委託和數據源到我的CollectionView 種我已經實現了以下兩種方法也

cellForItemAt indexPath numberOfItemsInSection

任何幫助將appriciated

+0

'類的MenuBar:UIView'其一個視圖中未'類的MenuBar:UIViewController' –

回答

3

這是因爲你試圖訪問自我封閉物

lazy var collectionView : UICollectionView = { 
    [unowned self] in 
    let layout = UICollectionViewFlowLayout() 
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    cv.backgroundColor = UIColor.rgb(red: 230, green: 32, blue: 31) 
    cv.delegate = self // Error here 

    return cv 

}() 

這應該工作

+0

謝謝@Evgeniy Gushchin –

相關問題