用xib创buildUICollectionViewCell子类
我想创build一个UICollectionViewCell子类与链接一个xib,我有这样做:我已经创build一个新的xib文件,我已经添加一个UICollectionViewCell ,然后我创build了这个子类文件: 
 @interface MyCell : UICollectionViewCell @property (weak, nonatomic) IBOutlet UILabel *label; @end 
 另外我已经在文件所有者自定义类中MyCell在界面生成器中的MyCell类,我已经添加了一个UILabel ,然后在我的UICollectionView viewDidLoad我这样做: 
 [self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"]; UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil]; [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"]; 
以及在这个:
 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; cell.label.text = @"Cell Text"; return cell; } 
但是这不起作用,我收到这个错误:
 *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x907eca0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.' 
 我做错了什么? 如何将UICollectionViewCell子类连接到xib,并将其显示在UICollectionView ? 
编辑:
我有这样做:
 - (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; } 
	
确保.xib文件中的单元格知道单元格的types。
在界面构build器中select单元格

然后在身份检查员身上

随后将您的标签与您的属性相关联。 (我想你已经这样做了)
 然后,我build议validation,如果你已经加载了你的cellForItemAtIndexPath:方法的cellForItemAtIndexPath:文件 
 NSString *identifier = @"MyCell"; static BOOL nibMyCellloaded = NO; if(!nibMyCellloaded) { UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil]; [cv registerNib:nib forCellWithReuseIdentifier:identifier]; nibMyCellloaded = YES; } 
你可以在这个文档中find答案UICollectionView添加UICollectionCell 。
只有StoryBoard里面的UICollectionView里面有UICollectionViewCell。 如果使用XIB,使用CellName.xib创build一个新的XIB,将CollectionViewCell添加到它,指定UICollectionView自定义类的名称。 之后使用registerNib.Sample代码: https : //github.com/lequysang/TestCollectionViewWithXIB