2017-02-02 6 views
1

Попытки инициализировать класс просмотра коллекции и получают следующее сообщение об ошибке:Должен назвать назначенный инициализатор суперкласса «UICollectionView»

Must call a designated initializer of the superclass 'UICollectionView'

данные передаются от другого ViewController, и я хочу, чтобы вытянуть кадр из в пределах 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

Вам необходимо позвонить назначенному инициализатору init(frame:collectionViewLayout:) из UICollectionView.

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 Приветствую вас, рад, что он работает для вас :) –