ios 使用 xib 创建 UICollectionViewCell 子类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14466959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:57:57  来源:igfitidea点击:

Create UICollectionViewCell subclass with xib

iosiphoneuicollectionviewxibuicollectionviewcell

提问by Piero

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

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

@interface MyCell : UICollectionViewCell

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

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

此外,我在文件所有者自定义类中链接了MyCell界面构建器中的类,并添加了一个UILabel,然后在我的UICollectionViewviewDidLoad 中执行以下操作:

[self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];

UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"];

As well 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 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.'

What did I do wrong? How can I connect a UICollectionViewCellsubclass to a xib, and display it in a UICollectionView?

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

EDIT:

编辑:

i have do this:

我已经这样做了:

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

回答by perrohunter

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

确保 .xib 文件中的单元格知道单元格的类型。

Select the cell on your interface builder

选择界面构建器上的单元格

enter image description here

在此处输入图片说明

and then on the identity inspector

然后在身份检查员身上

enter image description here

在此处输入图片说明

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

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

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

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

NSString *identifier = @"MyCell";    

    static BOOL nibMyCellloaded = NO;

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

回答by Autobots

You can find the answer in this document UICollectionView adding UICollectionCell.

您可以在本文档UICollectionView 添加 UICollectionCell 中找到答案。

Only UICollectionView inside StoryBoard have UICollectionViewCell inside. If use XIB, create a new XIB with CellName.xib, add CollectionViewCell to it, specify name of UICollectionView custom class. After that use registerNib.Sample code: https://github.com/lequysang/TestCollectionViewWithXIB

只有 StoryBoard 中的 UICollectionView 里面有 UICollectionViewCell。如果使用XIB,则使用CellName.xib 创建一个新的XIB,将CollectionViewCell 添加到其中,指定UICollectionView 自定义类的名称。之后使用 registerNib.Sample 代码:https: //github.com/lequysang/TestCollectionViewWithXIB