从 UICollectionViewCell 实例访问图像时遇到问题 [英] Trouble accessing Image from instance of UICollectionViewCell

查看:30
本文介绍了从 UICollectionViewCell 实例访问图像时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照本教程对我的项目进行一些修改:

我有一个名为 FeedViewCell 的自定义类,它包含一个 UIImageView.这是 cellForItemAtIndexPath 代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{静态 NSString *identifier = @"Cell";UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];imageView.image = [UIImage imageNamed:[postPhotos objectAtIndex:indexPath.row]];返回单元格;}

问题是 UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];返回为零.无论如何,使用 viewWIthTag 方法对我来说似乎很奇怪,但是在调试器中我看不到 imageView 是 UICollectionViewCell 的子视图的迹象.如果您查看此调试屏幕,您会发现该单元格似乎没有任何 UIImageView 子视图:

但是我看到 2 个 UIImageViews 作为 CollectionView 的子视图.

所以看起来我在 IB 中做错了什么.这并不奇怪,因为我似乎总是在与 IB 斗争(至少看代码我可以看到发生了什么).

感谢您的建议!

更新:我放弃了使用 IB 挂钩 ImageView 并尝试在代码中创建它,如下所示:http://cl.ly/QFxs

图像无法正常显示.不过,如果您查看屏幕截图(调试器),您会看到图像和图像视图都是有效对象.

解决方案

如果您正在调用 self.collectionView registerClass...,则需要将其删除.故事板会自动处理此注册.

I'm following this tutorial with some modifications for my project: http://www.appcoda.com/ios-programming-uicollectionview-tutorial/

I'm trying to get the instance of UIImageView that IB creates for me.

Here is a screenshot of my IB:

I have a custom class called FeedViewCell that is to contain an UIImageView. Here is the cellForItemAtIndexPath code:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
    imageView.image = [UIImage imageNamed:[postPhotos objectAtIndex:indexPath.row]];

    return cell;
}

The problem is that UIImageView *imageView = (UIImageView *)[cell viewWithTag:100]; comes back as nil. Using the viewWIthTag method seems weird to me anyway, but in the debugger I see no sign that imageView is a subview of UICollectionViewCell. If you look at this debug screen, you can see that the cell doesn't appear to have any UIImageView subviews:

However I see 2 UIImageViews as subviews of the CollectionView.

So it seems to be that I'm doing something wrong in IB. This isn't surprising as I always seem to struggle with IB (looking at code at least I can see what's going on).

Thanks for any suggestions!

update: I gave up on using IB to hook in the ImageView and tried creating it in code as below: http://cl.ly/QFxs

The images don't display properly. If you look in the screenshot (debugger) though, you will see images and imageViews are both valid objects though.

解决方案

If you are calling self.collectionView registerClass..., then you need to remove it. Storyboards handle this registration automatically.

这篇关于从 UICollectionViewCell 实例访问图像时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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