使用 xib 创建 UICollectionViewCell 子类 [英] Create UICollectionViewCell subclass with xib

查看:33
本文介绍了使用 xib 创建 UICollectionViewCell 子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个带有链接 xib 的 UICollectionViewCell 子类,我已经这样做了:我创建了一个新的 xib 文件,并在其中添加了一个 UICollectionViewCell,然后我创建了这个子类文件:

I'm trying to create a UICollectionViewCell subclass with linked a xib, I have do this: I have create a new xib file and I have add a UICollectionViewCell in it, then I have create this subclass file:

@interface MyCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UILabel *label;
@end

我还在文件所有者自定义类中链接了界面生成器中的 MyCell 类,并且我添加了一个 UILabel,然后在我的 UICollectionView viewDidLoad 我这样做:

Also I have linked in the file owner custom class the MyCell class in interface builder, and I have added a UILabel, then in my UICollectionView viewDidLoad I do this:

[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;
}

但是这不起作用,我收到此错误:

However this doesn't work, I receive 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.'

我做错了什么?如何将 UICollectionViewCell 子类连接到 xib,并将其显示在 UICollectionView 中?

What did I do wrong? How can I connect a UICollectionViewCell subclass to a xib, and display it in a 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 文件中的单元格知道该单元格的类型.

Make sure the cell on the .xib file know what's the type of the cell.

选择界面生成器上的单元格

Select the cell on your interface builder

然后是身份检查员

随后将您的标签与您的属性相关联.(我想你已经这样做了)

Subsequently associate your labels with your properties. (I think you already did that)

然后我建议验证您是否已经在 cellForItemAtIndexPath: 方法中加载了 .xib 文件

Then I'd recommend to verify if you already loaded the .xib file on your cellForItemAtIndexPath: method

NSString *identifier = @"MyCell";    

    static BOOL nibMyCellloaded = NO;

    if(!nibMyCellloaded)
    {
        UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil];
        [cv registerNib:nib forCellWithReuseIdentifier:identifier];
        nibMyCellloaded = YES;
    }

这篇关于使用 xib 创建 UICollectionViewCell 子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆