集合视图单元格在图像视图中不显示图像 [英] Collection View Cell does not display image in image view

查看:30
本文介绍了集合视图单元格在图像视图中不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 iOS 应用程序创建一个简单的图库.我正在使用 ViewController 中的 UICollectionView.一切似乎都工作正常,但我无法获得我试图用来在集合视图单元格内显示的图像.我将在这里转储一些代码,我一直在急切地想找到解决方案,但似乎没有任何效果.

I am trying to create a simple gallery for an iOS application. I'm working with a UICollectionView inside of a ViewController. 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.

这是gallery.m 文件:

This is the gallery.m file:

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

这是 customCell.h 文件(UIImageView 插座连接到集合视图单元格内的图像视图):

This is the customCell.h file (the UIImageView outlet is connected to the image view inside of the collection view cell):

#import <UIKit/UIKit.h>

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

@end

关于故事板中的连接,一切都应该没问题,也很难在这里展示,但无论如何我都会尝试解释.UICollectionView 连接在 gallery.h 文件中,如下所示:

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:

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

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


@end

故事板中的集合视图单元格具有自定义类customCell"和标识符customCell".

The collection view cell in the storyboard has the custom class "customCell" and identifier "customCell".

如果有人能指出我的错误所在,那就太棒了!谢谢!

If anybody could point me to where my mistake could be it would be awesome! Thank you!

推荐答案

在故事板中查看,有时在单元格中,xCode 将图像帧设置为 (0,0,0,0).它让我发疯了很多次^^

Look in the storyboard, sometimes in cells, xCode set images frame to (0,0,0,0). It made me crazy a lot of times ^^

要创建自定义集合单元,请创建一个空的 XIB 文件.随心所欲地在其中添加一个 collectionViewCell .与您建立连接 .h 文件.

EDIT : To create a custom collection cell, create an empty XIB file. Add a collectionViewCell in it with all you want. Make the connections with you .h file.

在你的 viewController -> viewDidLoad 方法中添加

In your viewController -> viewDidLoad method add

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

在 cellForRowAtIndexPath 中

And in cellForRowAtIndexPath

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

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

这篇关于集合视图单元格在图像视图中不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆