Reputation: 83
I have a view controller which contains collection view. Collection view's delegate and datasource are one custom class and I can't access Collection View itself from that class. The class includes UICollectionViewFlowLayout, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource.
I tried accessing the collection view by typing self.collectionView which didn't work, saying: "fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)".
Tried setting a static let variable which points to View Controller containing collection view but it also didn't work.
Upvotes: 1
Views: 614
Reputation: 1127
Delegates and datasources are dataprovider, not more. Extracting them into own classes is a very good idea (object oriented design). But these classes shouldn't implement more than the api of the protocol and maybe some private helper functions for them. Each protocol function provide access to your collection view by giving a reference as the first parameter.
e.g.
func numberOfSections(in: UICollectionView)
Upvotes: 1