Subclass UICollectionViewCell with xib

I am trying to subclass a UICollectionViewCell with associated xib, I do this: I created a new xib file and I added a UICollectionViewCell to it, then I created this subclass file:

 @interface MyCell : UICollectionViewCell @property (weak, nonatomic) IBOutlet UILabel *label; @end 

I also linked the MyCell class in the interface constructor in the user class of the file owner, and I added UILabel , and then I do this in my UICollectionView viewDidLoad:

 [self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"]; UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil]; [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"]; 

As in this:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; cell.label.text = @"Cell Text"; return cell; } 

However, this will not work, I get this error:

 *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x907eca0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.' 

What did I do wrong? How to subclass UICollectionViewCell to xib and display it in UICollectionView ?

EDIT:

I'm doing it:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = @"MyCell"; static BOOL nibMyCellloaded = NO; if(!nibMyCellloaded) { UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil]; [cv registerNib:nib forCellWithReuseIdentifier:identifier]; nibMyCellloaded = YES; } MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; cell.labelCell.text = @"Text"; return cell; } 
+45
ios iphone xib uicollectionview uicollectionviewcell
Jan 22 '13 at 19:58
source share
2 answers

Make sure the cell in the .xib file knows what type of cell.

Select a cell in the interface builder

enter image description here

and then in the identity inspector

enter image description here

Subsequently associate your tags with your properties. (I think you already did it)

Then I would recommend checking if the .xib file is uploaded using your cellForItemAtIndexPath: method cellForItemAtIndexPath:

 NSString *identifier = @"MyCell"; static BOOL nibMyCellloaded = NO; if(!nibMyCellloaded) { UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil]; [cv registerNib:nib forCellWithReuseIdentifier:identifier]; nibMyCellloaded = YES; } 
+48
Jan 22 '13 at 21:03
source share

You can find the answer in this UICollectionView document adding a UICollectionCell .

Only the UICollectionView inside the StoryBoard has the UICollectionViewCell inside. If you use XIB, create a new XIB with CellName.xib, add CollectionViewCell to it, specify the name of the user class UICollectionView. After that use registerNib.Sample code: https://github.com/lequysang/TestCollectionViewWithXIB

+20
May 22 '13 at 14:02
source share



All Articles