简体   繁体   English

UICollectionView单元格中的UIImageView不显示

[英]UIImageView in UICollectionView cells not displaying

I'm very new to iOS development, sorry for posting such a basic mistake, I just can't get my head around this. 我是iOS开发的新手,很抱歉发布这样的基本错误,但我还是无法解决。

I will tell you what I have done so far, hopefully someone can help me out here. 我会告诉你到目前为止我做了什么,希望有人可以在这里帮助我。 I have not edited any code so far. 到目前为止,我还没有编辑任何代码。

  1. Add collection view controller (click and drag into main.storyboard) 添加集合视图控制器(单击并拖动到main.storyboard中)
  2. Configure cell sizes in the attributes editor of the CollectionView 在CollectionView的属性编辑器中配置单元格大小
  3. Drag UIImageView object onto each cell 将UIImageView对象拖到每个单元格上
  4. Import my images into the project (drag images into sidebar and import) 将我的图像导入到项目中(将图像拖到侧边栏并导入)
  5. Select each UIImageView and in attributes select the correct image 选择每个UIImageView并在属性中选择正确的图像

Having done this, my images display perfectly in the storyboard editor but when I run the app in the simulator it is just a blank screen with my specified background colour. 完成此操作后,我的图像可以在情节提要编辑器中完美显示,但是当我在模拟器中运行该应用程序时,它只是一个具有指定背景色的空白屏幕。

Screenshot of editor , Screenshot of simulator 编辑器的 截图,模拟器的截图

You should add the data source method after creating a collection view controller class, 您应该在创建集合视图控制器类之后添加数据源方法,

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

You should implement datasource methods for displaying a cell. 您应该实现用于显示单元格的数据源方法。 Doesn't meter if it's static or dynamic cells. 不测量是静态还是动态单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM