xcode UICollectionView 单元笔尖未加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16347897/
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-09-15 03:19:15  来源:igfitidea点击:

UICollectionView Cell nib not loading

iphoneobjective-cxcodeuicollectionviewuicollectionviewcell

提问by isal

I'm using a custom subclass of UICollectionViewFlowLayoutthat allows reordering of cells. Source code here.

我正在使用UICollectionViewFlowLayout允许重新排序单元格的自定义子类。源代码在这里

The issue I am running into is that the nib file I created for the Collection View does not load despite registering it and using the correct identifier. Here are the relevant code snippets.

我遇到的问题是,尽管注册并使用了正确的标识符,但我为集合视图创建的 nib 文件没有加载。以下是相关的代码片段。

This is in the view controller .mfile:

这是在视图控制器.m文件中:

- (void)loadView
{
    self.reorderLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero
                                             collectionViewLayout:self.reorderLayout];

    UINib *nib = [UINib nibWithNibName:@"CatagoryViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"CatagoryViewCellID"];

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Catagory *c = [[[CatagoryStore defaultStore] allCatagories]
                   objectAtIndex:[indexPath item]];

    CatagoryViewCell *cell = (CatagoryViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CatagoryViewCellID" forIndexPath:indexPath];

    [cell setController:self];

    [[cell nameLabel] setText:c.title];
    return cell;
}

These are the only two places I reference the nib.

这是我提到笔尖的仅有的两个地方。

Here is the cell nib: enter image description here

这是细胞笔尖: 在此处输入图片说明

This is what it looks like in the simulator and device:

这是模拟器和设备中的样子:

enter image description here

在此处输入图片说明

I tried using similar code and the original UICollectionViewFlowLayoutclass and it works fine. Any insight is greatly appreciated!

我尝试使用类似的代码和原始UICollectionViewFlowLayout类,它工作正常。非常感谢任何见解!

回答by Angel Gzid

Try with this "initWithFrame" methods inside your CollectionViewCell.m

尝试在 CollectionViewCell.m 中使用此“initWithFrame”方法

-(id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];

if (self) {
    NSString *cellNibName = @"YourCollectionViewCell";
    NSArray *resultantNibs = [[NSBundle mainBundle] loadNibNamed:misVentasCellNibName owner:nil options:nil];

    if ([resultantNibs count] < 1) {
        return nil;
    }

    if (![[resultantNibs objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
        return nil;
    }
    self = [resultantNibs objectAtIndex:0];
}

return self;    
}

Then in the class you have de collection view add:

然后在类中你有 de collection view 添加:

[collectionView registerClass:[YourCollectionViewCell class] forCellWithReuseIdentifier:@"YourCollectionViewCell"];

And Finally

最后

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

// Setup cell identifier
static NSString *cellIdentifier = @"YourCollectionViewCell";

YourCollectionViewCell *cell = (YourCollectionViewCell *)[cvArticles dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
}

Remember you must insert in your.xib the same cellIdentifier you are using in the Identifier field of the Collection view!!!!!!!!

请记住,您必须在您的.xib 中插入您在 Collection 视图的 Identifier 字段中使用的相同 cellIdentifier!!!!!!!!!