2017-02-02 9 views
1

コレクションビュークラスを初期化しようとすると、次のエラーを取得:は、スーパークラスのUICollectionView "の指定イニシャライザを呼び出す必要があります

データは別のViewControllerから渡されていると私はからフレームを引き出したい

Must call a designated initializer of the superclass 'UICollectionView'

ユーザーが検索したときにViewController内に表示されます。

class searchLocationsCollectionViewManager: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate { 

weak var collectionView: UICollectionView! { 
    didSet { 
     collectionView.dataSource = self 
     collectionView.delegate = self 
    } 
} 

// Data handler 
var data: [[String:Any]]? { 
    didSet { 
     print(data!) 
     //collectionView.reloadData() 
    } 
} 

init(collectionView: UICollectionView, frame: CGRect) { 
    self.collectionView = collectionView 
    super.init() 
} 

required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder)! 
} 



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

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

// This is the current method for returning the cell. 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 




    return UICollectionViewCell() 

} 

上記の理由が考えられますか?

+0

'UICollectionView'サブクラスの' UICollectionView'プロパティの目的は何ですか?初期値が割り当てられたときに 'didSet'が呼び出されないことを考慮してください。そして 'UICollectionViewController'を意味する場合には、すでに' UICollectionView'プロパティがあります。 – vadian

答えて

3

UICollectionViewの指定された初期化子init(frame:collectionViewLayout:)に電話する必要があります。

init(collectionView: UICollectionView, frame: CGRect) {   
    self.collectionView = collectionView 
    //Setup collectionView layout here and pass with init 
    let layout = UICollectionViewLayout() 
    super.init(frame: frame, collectionViewLayout: layout) 
} 
+0

ありがとう!このソリューションは機能しました! –

+0

@JesseCようこそメイト、あなたのためにうれしい:) –

関連する問題