Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unrecognized selector sent to instance 0x8c9a6d0

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

like image 997
Rasmus Kirkeby Borup Avatar asked Aug 23 '14 12:08

Rasmus Kirkeby Borup


4 Answers

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
}
like image 153
Egzon P. Avatar answered Oct 24 '22 01:10

Egzon P.


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

like image 42
Daij-Djan Avatar answered Oct 24 '22 01:10

Daij-Djan


Put the protocols : UICollectionViewDelegate and UICollectionViewDataSource after UIViewController to access UICollectionView delegate methods.

like image 24
Nupur Sharma Avatar answered Oct 24 '22 02:10

Nupur Sharma


also it could be you have forgotten delegate methods after class name

class YourClassName: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
}
like image 37
Omar N Shamali Avatar answered Oct 24 '22 01:10

Omar N Shamali