2017-02-02 44 views
1

试图初始化集合视图类,并收到以下错误:必须调用父类的UICollectionView'的指定初始化

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欢迎队友,很高兴它适合你:) –

相关问题