我正在按照本教程对我的项目进行一些修改:http://www.appcoda.com/ios-programming-uicollectionview-tutorial/ http://www.appcoda.com/ios-programming-uicollectionview-tutorial/
我正在尝试获取 IB 为我创建的 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 *)[cell viewWithTag:100];返回为零。无论如何,使用 viewWIthTag 方法对我来说似乎很奇怪,但在调试器中我没有看到 imageView 是 UICollectionViewCell 子视图的迹象。如果您查看此调试屏幕,您可以看到该单元格似乎没有任何 UIImageView 子视图:
但是我看到 2 个 UIImageViews 作为 CollectionView 的子视图。
所以看来我在IB中做错了什么。这并不奇怪,因为我似乎总是与 IB 作斗争(查看代码至少我可以看到发生了什么)。
感谢您的任何建议!
更新:我放弃使用 IB 挂钩 ImageView 并尝试在代码中创建它,如下所示:http://cl.ly/QFxs http://cl.ly/QFxs
图像无法正常显示。如果您查看屏幕截图(调试器),您将看到图像和 imageViews 都是有效的对象。