自定义UICollectionViewCell中的UIImageView始终返回nil

24

在我的故事板中,我使用了一个带有3个单元格的UICollectionView。其中一个单元格,我为其创建了一个自定义类,该类显然是UICollectionViewCell的扩展:

enter image description here

我在ViewDiDApear中注册了这个类:

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

我在自定义的单元格中添加了一个imageView,并在自定义类中添加了它的outlet。问题出现在我制作自定义单元格时,它忘记了在storyboard中设置的所有内容,比如背景颜色和图像视图等。由于这个问题,我的imageView一直返回nil。

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

    if(indexPath.row == 0)
    {
        static NSString *identifier = @"Cell1";
        DeviceImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        UIImage *image = [UIImage imageNamed:@"dysart-woods-full.jpg"];
        cell.imageview.image = image;
        cell.backgroundColor = [UIColor blueColor];

        //cell.imageview.image = image;
        return cell;
    }
    else if(indexPath.row == 1)
    {
        static NSString *identifier = @"Cell2";
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        return cell;
    }
    else
    {
        static NSString *identifier = @"Cell3";
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
        imageView.image = [UIImage imageNamed:@"dysart-woods-full.jpg"];

        return cell;
    }
}

1
这里也有同样的问题,但还没有解决方案。@Dmytro的答案不幸地没有帮助到我。我在同一个自定义单元格中设置了UILabel上的文本,它可以正常工作,但是CollectionView中的图像却无法显示。 - race_carr
你解决过这个问题吗?我在一个新项目中遇到了完全相同的问题。我之前在集合视图单元格中放置了图像视图,没有任何问题...但出于某种原因,无论我是在故事板还是在nib中创建单元格,ImageView = nil,而UILabels却正常工作。@#%)&(* - MobileVet
6个回答

53

你可能早已解决了这个问题。

但是对于那些遇到相同问题的人,

在你的viewDidAppear中调用registerClass函数即可。

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

你已经在Interface Builder中注册了该类,因此调用registerClass:forCellWithReuseIdentifier:正在替换IB创建的条目。

删除这一行将解决问题。


哎呀我简直不敢相信原来如此简单。那为什么在创建 UICollectionViewController 时 Xcode 自动添加这行代码呢?:( - Benoît
2
如果使用nib,则需要使用以下代码进行注册:[self.collectionView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCellIdentifier"]; - atulkhatri
非常感谢这个。 - Gary O' Donoghue

12

如果您正在使用自定义单元格,那么应该注册 nib,如下所示 -

因此它将是:

[self.collectionView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCellIdentifier"];

而不是:

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

2

嗯,这很奇怪。我知道一个解决方案,但我不太明白为什么它会有所不同。我的设置和问题完全相同。UIImageView的IBOutlet被设置为nil,但UILabel没有。

将UIImageView的声明更改为强属性而不是实例变量。

@property (strong) IBOutlet UIImageView* imageView;

这解决了我的问题。显然UIImageView从storyboard中连接/实例化的方式存在问题。另一个“奇怪”的因素是,我在UITableViewCell中使用相同的ivar设置,没有问题,但在UICollectionViewCell中会出现问题。


1
我曾遇到类似的问题,通过在DeviceImageCell中添加以下代码已解决。
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        _imageview = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
        [self.contentView addSubview:_imageview];
    }
    return self;
}

很遗憾,如果没有这段代码的添加,我无法使_imageview初始化。

0

还要检查这一个,

所以现在,当你在故事板中声明自定义CollectionView单元格时,你需要在可重用的集合视图下指定名为“标识符”的属性到一些字符串。 现在这听起来可能有点奇怪,但是请检查标识符的名称是否与集合视图单元格的类名称不同。 单元格的类/文件名称和单元格标识符必须不同。

这对我真的起作用了,因此发布。


-1
如果你正在使用故事板,那么请删除下面这行代码。
collectionview.register(CollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell")

请不要发布重复的回答。 - HangarRash
请不要发布重复的答案。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接