自定义UICollectionViewCell中的UIImageView始终返回nil [英] UIImageView in custom UICollectionViewCell returning nil all the time

查看:640
本文介绍了自定义UICollectionViewCell中的UIImageView始终返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

OK in my story board I have made a UICollectionView with 3 cells. One of the cells I made a custom class for that obviously extends the UICollectionViewCell:

我在ViewDiDApear中注册了该课程:

And I registered the class in my ViewDiDApear:

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

此外,我还在我的自定义类中为该特定单元格添加了一个imageView,并为该imageView添加了一个插座。当我制作自定义单元格时会出现问题,它会忘记我的故事板中设置的所有内容,即背景颜色,我的图像视图等等。因此,我的imageView始终返回nil。

Also I have added an imageView to that specific cell and an outlet for that imageView in my custom class. Problem occurs when I make my custom cell it forgets everything that was set in my storyboard, aka the background color, where my image view is and so on. Because of this my imageView returns nil all the time.

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


推荐答案

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

You have probably long solved this issue.

然而,对于那些发现这个并且有相同问题的人

However for those finding this and having the same issue

这是 registerClass 中调用viewDidAppear

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

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

You are already registering the class in Interface Builder, so the call to registerClass:forCellWithReuseIdentifier: is replacing the entry created by IB.

删除此行将解决问题。

这篇关于自定义UICollectionViewCell中的UIImageView始终返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆