无法从UICollectionViewCell实例访问Image
我正在跟随本教程对我的项目进行一些修改: http : //www.appcoda.com/ios-programming-uicollectionview-tutorial/
我试图得到IB为我创build的UIImageView的实例。
这是我的IB的截图:
我有一个名为FeedViewCell的自定义类,它包含一个UIImageView。 这是cellForItemAtIndexPath代码:
- (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; }
问题是,UIImageView * imageView =(UIImageView *)[单元viewWithTag:100]; 返回为零。 使用viewWIthTag方法似乎对我来说是不可思议的,但在debugging器中,我看不到imageView是UICollectionViewCell的子视图。 如果你看看这个debugging屏幕,你可以看到单元格没有任何UIImageView子视图:
但是我看到2个UIImageViews作为CollectionView的子视图。
所以这似乎是我在IB做错了事。 这并不奇怪,因为我似乎总是与IB斗争(至less看代码我可以看到发生了什么)。
感谢您的任何build议!
更新:我放弃了使用IB挂钩ImageView,并尝试在代码中创build它,如下所示: http : //cl.ly/QFxs
图像无法正确显示。 如果你看屏幕截图(debugging器),你会看到图像和imageViews都是有效的对象。
在这种情况下我不认为你需要使用标签。 您可以在FeedViewCell中创build一个UIImageView属性,然后在界面生成器中进行连接,然后访问它
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
例如
//在FeedViewCell中
@interface FeedViewCell : UICollectionViewCell @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end
//在控制器中
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"Cell"; FeedViewCell *cell = (FeedViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; cell.imageView.image = [UIImage imageNamed:[postPhotos objectAtIndex:indexPath.row]]; return cell; }
打开故事板,然后单击助理编辑器button,将调出另一个窗口。 在那里打开feedViewCell.h,然后按住Ctrl点击imageView并将其拖动到.h文件中,该文件将为您提供一个菜单来创build插口。 您可以将它命名为imageView属性。 应该是这样的。
看看这个链接了解更多信息http://klanguedoc.hubpages.com/hub/IOS-5-A-Beginners-Guide-to-Storyboard-Connection
如果你正在调用self.collectionView registerClass...
,那么你需要删除它。 故事板自动处理此注册。
- 如何通过button显示图像? -迅速
- Xcode 6.1 dyld:Library未加载:@ rpath / XCTest.framework / XCTest错误
- CALayer上的hitTest – 你如何找到被击中的实际图层?
- 在WKWebView中禁用整个UIMenuController编辑菜单
- 如何将导航栏放在相机框架的顶部?
- 当从UIBarButtonItem呈现方向更改后,如何防止UIPopoverController passthroughViews被重置?
- Swift 3.0中的Any vs.AnyObject
- 核心数据连接表中有许多通过获取属性的谓词
- 如何合并两个UIImages,同时保持其位置,大小和纵横比?