I have ViewController called myVC with UITablewView - myTable.
What I want is to add some UIView as myTable's headerView from code. So inside viewDidLoad() method of myVC I added this code
let topView = TopView()
topView.frame.size.height = 100
topView.frame.size.width = myTable.frame.width
myTable.tableHeaderView = featuredEventsView
I also created file called TopView.swift that looks something like
class TopView : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {.....}
}
And it is working as it should. I see red UIView in headerView of myTable.
Now I want to add UICollectionView inside topView and I have problems here. I am trying to do something like
class TopView : UIView, UICollectionViewDataSource, UICollectionViewDelegate {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .red
addSubview(myCollectionView)
}
required init?(coder aDecoder: NSCoder) {.....}
let myCollectionView : UICollectionView = {
let cv = UICollectionView()
cv.translatesAutoresizingMaskIntoConstraints = false
cv.delegate = self as! UICollectionViewDelegate
cv.dataSource = self as! UICollectionViewDataSource
cv.backgroundColor = .yellow
return cv
}()
}
I also created functions needed to UICollectionViewDataSource but app crashes after building. What am I doing wrong?