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

查看:96
本文介绍了使用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天全站免登陆