【问题标题】:UIImageView in UICollectionView cells not displayingUICollectionView 单元格中的 UIImageView 不显示
【发布时间】:2014-09-06 15:52:09
【问题描述】:

我是 iOS 开发的新手,很抱歉发布了这样一个基本错误,我无法理解这一点。

我会告诉你到目前为止我做了什么,希望有人可以帮助我。到目前为止,我还没有编辑任何代码。

  1. 添加集合视图控制器(单击并拖入 main.storyboard)
  2. 在 CollectionView 的属性编辑器中配置单元格大小
  3. 将 UIImageView 对象拖到每个单元格上
  4. 将我的图片导入到项目中(将图片拖到侧边栏并导入)
  5. 选择每个 UIImageView 并在属性中选择正确的图像

完成此操作后,我的图像在情节提要编辑器中完美显示,但当我在模拟器中运行应用程序时,它只是一个带有我指定背景颜色的空白屏幕。

Screenshot of editor, Screenshot of simulator

【问题讨论】:

  • 您确定您使用的是静态而不是动态单元格吗?您的控制器不应实现数据源收集方法。

标签: ios uiimageview uicollectionview xcode-storyboard


【解决方案1】:

你应该在创建集合视图控制器类之后添加数据源方法,

// Defines the number of cells in a section of collection view
- (NSInteger)collectionView:(UICollectionView *)cv numberOfItemsInSection:(NSInteger)section;
{
    return numberOfItems;
}

// Defines the cells in the collection view
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
// Gallerycell is the custom collection view cell class holds the UIImage View
    GalleryCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"GalleryCell" forIndexPath:indexPath];
// getting the image 
    cell.cellImage.image = [UIImage imageNamed:[dataSourceArray objectAtIndex:indexPath.row]];
    return cell;
}

【讨论】:

  • 感谢您的帮助。不过我不太明白 - 我是否创建一个新类,它是集合视图控制器的子类,然后将您的代码放入它的 .m 文件中?我还需要为 GalleryCell 开设一个课程吗?
  • 是的,你是对的。在图库单元格中为图像视图添加 IBOutlet 并将其与集合视图单元格中的图像视图连接起来。在上面的 cellForItemAtIndexPath 方法中设置此图像
  • 好的,我已经按照你的建议实现了,但是它抛出了一个错误:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier XYZGalleryCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
  • 在故事板或 nib 中添加与 cellForItemAtIndexPath 中指定的标识符相同的单元格标识符,即 GalleryCell 检查此链接i.imgur.com/TAIrOri.png
【解决方案2】:

您应该实现显示单元格的数据源方法。如果它是静态或动态单元格,则不计量。

【讨论】:

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