Wednesday, December 12, 2012

CollectionViewCell - Custom Cell implementation error

When i try to create custom view for collection view i got the following error, even though i connected all controls properly inside the collection viewcell.

2012-12-12 19:14:50.121 sMatcher[13647:11303] porutham cell <NSIndexPath 0x903aab0> 2 indexes [0, 0]

2012-12-12 19:14:50.123 sMatcher[13647:11303] -[UICollectionViewCell lblTitel]: unrecognized selector sent to instance 0x903ca50

2012-12-12 19:14:50.123 sMatcher[13647:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell lblTitel]: unrecognized 

Later i have created seperate class file with xib specifically for cell.

I have designed the cell in xib, but still i get the same error above.

Then with reference to this thread

I have changed my code in viewDidLoad of the main controlller like below

Instead of this

 [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"poruthamCellV"];

I have changed to this

[self.collectionView registerNib:[UINib nibWithNibName:@"poruthamCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"poruthamCellV"];


All started working as i expected, what ever i have designed in xib started appearing in the collection view. So seems like we have to register the cell xib file before we use in collection view which is something different from custom table view cell implementation where we no need to do this.

No comments: