【问题标题】:UIImageView in custom UICollectionViewCell returning nil all the time自定义 UICollectionViewCell 中的 UIImageView 一直返回 nil
【发布时间】:2018-03-08 14:17:11
【问题描述】:

好的,在我的故事板中,我制作了一个包含 3 个单元格的 UICollectionView。我为其创建自定义类的单元之一显然扩展了 UICollectionViewCell:

我在我的 ViewDiDApear 中注册了该课程:

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

我还在我的自定义类中为该特定单元格添加了一个 imageView 和一个该 imageView 的出口。当我制作自定义单元格时会出现问题,它会忘记在我的故事板中设置的所有内容,也就是背景颜色,我的图像视图所在的位置等等。因此,我的 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;
    }
}

【问题讨论】:

  • 同样的问题,目前还没有解决办法。不幸的是,@Dmytro 的回答没有帮助。我在同一个自定义单元格中的 UILabel 上设置了文本,并且可以正常工作,但图片没有出现在 CollectionView 中。
  • 你解决过这个问题吗?我在一个新项目中遇到了完全相同的问题。我之前将图像视图放在集合视图单元格中没有问题......但出于某种原因,无论我在情节提要还是在笔尖中创建单元格,ImageView = nil 但 UILabels 工作正常。 @#%)&(*

标签: ios objective-c uicollectionviewcell


【解决方案1】:

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

但是对于那些发现此问题并遇到相同问题的人

这是您的viewDidAppear 中的registerClass 调用

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

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

删除此行将解决问题。

【讨论】:

  • 天哪,我不敢相信它这么简单。那为什么 Xcode 在创建 UICollectionViewController 时会自动添加这一行? :(
  • 如果是 nib,您必须使用以下方法注册 nib:[self.collectionView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCellIdentifier"];
  • 非常感谢
【解决方案2】:

如果您使用这样的单元格,您应该注册自定义单元格的nib-

原来是这样:

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

代替:

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

【讨论】:

  • 谢谢,拯救了我的一天;)
【解决方案3】:

嗯,这很奇怪。我知道一个解决方案,但我不太明白为什么它会有所作为。我有相同的确切设置和相同的问题。 UIImageView 的 IBOutlet 设置为 nil,而不是 UILabels。

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

@property (strong) IBOutlet UIImageView* imageView;

这为我解决了这个问题。显然,UIImageView 从情节提要中连接/实例化的方式存在一些问题。作为一个额外的“奇怪”因素,我使用与 UITableViewCell 相同的 ivars 设置并且没有问题,仅在 UICollectionViewCell 中

【讨论】:

    【解决方案4】:

    我遇到了类似的问题,通过在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。

    【讨论】:

      【解决方案5】:

      也检查一下这个,

      因此,现在当您在其 Storyboard 中声明自定义 CollectionView 单元时,您需要将 Collection Reusable View 下名为“Identifier”的属性指定为某个字符串。 现在这听起来可能很奇怪,但是, 检查 Identifier 的名称是否与 Collection View Cell 的类名不同。 单元的类/文件名和单元标识符必须不同。

      这确实对我有用,因此发布了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-06
        • 1970-01-01
        相关资源
        最近更新 更多