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; }
ios iphone xib uicollectionview uicollectionviewcell
Piero Jan 22 '13 at 19:58 2013-01-22 19:58
source share