xibをリンクしたサブクラスを作成しようとしUICollectionViewCell
ています。これを実行します。新しいxibファイルを作成し、それにを追加してから、次のUICollectionViewCell
サブクラスファイルを作成します。
@interface MyCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
また、ファイル所有者のカスタムクラス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;
}