I want to implement UICollectionView with custom cells, and previously I have:
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout and inside I implemented
func numberOfSections(in collectionView: UICollectionView) -> Int func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell and it's working fine. Now I want to implement more functions related to cell selection, but I also want to conform to MVC pattern so I'm trying to move the UICollectionView implementation to another file. Then I have:
class ViewController: UIViewController { @IBoutlet var grid: UICollectionView! ovverride func viewDidLoad() { ... grid = GridView(grid.frame, GridLayout()) //GridLayout() is the layout file working fine } } and another file:
class GridView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { func numberOfSections(in collectionView: UICollectionView) -> Int {} func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {} func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {} } But then if running it'll give error:
collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance I checked the link here: error, and tried to do this in the ViewController:
grid.dataSource = grid as! UICollectionViewDataSource? grid.delegate = grid as! UICollectionViewDelegate? But it's not working. May I know how to fix this?
EDIT Class AppDelegate gives the error. And I've also tried to set the initializer of GridView:
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) { super.init(...) self.dataSource = self self.delegate = self } But it's not working.