简体   繁体   English

集合视图单元格不在图像视图中显示图像

[英]Collection View Cell does not display image in image view

I am trying to create a simple gallery for an iOS application. 我正在尝试为iOS应用程序创建一个简单的库。 I'm working with a UICollectionView inside of a ViewController. 我正在使用ViewController内部的UICollectionView。 Everything seems to be working fine, but I can't get the image I'm trying to use to display inside of the collection view cell. 一切似乎都工作正常,但是我无法获得要用于显示在集合视图单元格中的图像。 I will just dump some code here, I have been desperate to find a solution but nothing seems to work. 我只是在这里转储一些代码,我一直在拼命地寻找解决方案,但似乎没有任何效果。

This is the gallery.m file: 这是gallery.m文件:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
{
    //just a random value for testing
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:   (NSInteger)section
{
    //another random test value
    return 5;
}

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

    customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier
                                          forIndexPath:indexPath];
    //UIImage *image = [dataArray objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"skull.png"];

    //I tried all of the below code to get the image to show but nothing seemed to work
    //[cell.imageView setImage:[UIImage imageNamed:@"skull.png"]];
    //[cell.imageView setNeedsDisplay];
    //[cell.imageView reloadInputViews];
    return cell;
}

This is the customCell.h file (the UIImageView outlet is connected to the image view inside of the collection view cell): 这是customCell.h文件(UIImageView出口连接到集合视图单元格内部的图像视图):

#import <UIKit/UIKit.h>

@interface customCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

Concerning the connections in the storyboard everything should be fine, also it would be difficult to show here but I will try to explain anyways. 关于情节提要中的连接,一切都应该很好,这里也很难显示,但无论如何我都会尝试进行解释。 The UICollectionView is connected in the gallery.h file like so: UICollectionView像下面这样连接在gallery.h文件中:

@interface gallery : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;


@end

The collection view cell in the storyboard has the custom class "customCell" and identifier "customCell". 情节提要中的集合视图单元格具有自定义类“ customCell”和标识符“ customCell”。

If anybody could point me to where my mistake could be it would be awesome! 如果有人可以指出我的错误所在,那就太棒了! Thank you! 谢谢!

I had this very similar problem. 我有这个非常相似的问题。 I could not display image view inside uicollection view cell, when storyboard is used. 使用情节提要板时,无法在uicollection视图单元内显示图像视图。 I just removed the following code in ViewDidLoad of CollectionViewController: 我刚刚在CollectionViewController的ViewDidLoad中删除了以下代码:

//[self.collectionView registerClass:[CustomCollectionViewCell class]forCellWithReuseIdentifier:reuseIdentifier];

Now the ImageView is displayed inside cell. 现在,ImageView显示在单元格内部。

Look in the storyboard, sometimes in cells, xCode set images frame to (0,0,0,0). 在情节提要中(有时在单元格中)查看,xCode将图像帧设置为(0,0,0,0)。 It made me crazy a lot of times ^^ 很多次让我发疯^^

EDIT : To create a custom collection cell, create an empty XIB file. 编辑:要创建一个自定义收集单元格,请创建一个空的XIB文件。 Add a collectionViewCell in it with all you want. 在其中添加所有所需的collectionViewCell。 Make the connections with you .h file. 与您的.h文件建立连接。

In your viewController -> viewDidLoad method add 在您的viewController-> viewDidLoad方法中添加

[myCollectionView registerNib:[UINib nibWithNibName:@"collectionCellXIB" bundle:nil] forCellWithReuseIdentifier:myIdentifier];

And in cellForRowAtIndexPath 并在cellForRowAtIndexPath中

customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:myIdentifier forIndexPath:indexPath];

if(!cell)
{
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:myIdentifier forIndexPath:indexPath];
}

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

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