Can anyone tell me what is going on in this error code
[UIViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x8c9a6d0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x8c9a6d0'
the collectionView code is
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [array count];
}
what have i done wrong
To fix unrecognized selector sent to instance 0x8c9a6d0, you must respect two rules:
1) You have to confirm that all IBOutlet connections exist in cell class. Confirm IBOutlet connection by clicking an item in your cell, for ex. Title label and in the right side of XCode click "Show the Connections inspector" round icon with right arrow "->". You you have many connections and you are confused, please remove all connections and connect them again in cell class.
2) You have to add delegates and datasource for that collectionView in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
PS. If you have uicollectionview cell inside tableview cell you must add delegates and datasource inside that cell class, adding with storyboard with drag and not seems not working on Xcode 10.1.
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
well, seems you don't set the right instance as datasource.
the runtime is trying to call the method on a UIViewController. That is wrong as that cannot be the right class.
my guess is that you haven't set your view controller's class in the xib and therefore a default UIViewController is used
Put the protocols : UICollectionViewDelegate and UICollectionViewDataSource after UIViewController to access UICollectionView delegate methods.
also it could be you have forgotten delegate methods after class name
class YourClassName: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With