I've created a generic UIView class which contains a UICollectionView inside of it. Just as below. (Class Below also handles the protocols of UICollectionView with Default values)
class MyCollectionView: BaseView<CollectionViewModel> {
private lazy var myCollectionView: UICollectionView = {
let temp = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewLayout()) // setting initial collectionView
temp.translatesAutoresizingMaskIntoConstraints = false
temp.delegate = self
temp.dataSource = self
temp.register(CollectionViewCell.self, forCellWithReuseIdentifier: CollectionViewCell.identifier)
temp.clipsToBounds = true
return temp
}()
}
I've created an instance of MyCollectionView(class above), and added as subview to the MainViewController(Class Below). So doing that made me show a MyCollectionView as a subview of MainViewController. I've accomplished so far.
class MainViewController: UIViewController {
private lazy var collectionView: MyCollectionView = {
let temp = MyCollectionView()
temp.translatesAutoresizingMaskIntoConstraints = false
temp.backgroundColor = .black
return temp
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
setUpConstraintsAndViews()
// Do any additional setup after loading the view.
}
Later on I tried to make UICollectionViewCell class and register that to myCollectionView. But still I can not see any cells on my screen. What might I could be missing?
CodePudding user response:
Your MyCollectionView
class is not a UICollectionView
. It does not have a subview of a UICollectionView
it has a private variable myCollectionView
that creates a collection view.
As far as I can tell you're adding an instance of MyCollectionView
as a subview of your MainViewController
. I don't see how that adds a UICollectionView
to your view MainViewController
.
How is the consumer of your MyCollectionView
supposed to get to the collection view that it owns?