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

Ios 集合视图单元格不在图像视图中显示图像,ios,objective-c,uiimageview,uicollectionview,Ios,Objective C,Uiimageview,Uicollectionview,我正在尝试为iOS应用程序创建一个简单的库。我正在使用ViewController中的UICollectionView。一切似乎都很好,但我无法获取我试图用于在“集合视图”单元格内显示的图像。我将在这里转储一些代码,我一直在不顾一切地寻找解决方案,但似乎什么都不起作用 这是gallery.m文件: -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView; { //just a rand

我正在尝试为iOS应用程序创建一个简单的库。我正在使用ViewController中的UICollectionView。一切似乎都很好,但我无法获取我试图用于在“集合视图”单元格内显示的图像。我将在这里转储一些代码,我一直在不顾一切地寻找解决方案,但似乎什么都不起作用

这是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;
}
这是customCell.h文件(UIImageView出口连接到集合视图单元格内的图像视图):

#导入
@接口customCell:UICollectionViewCell
@属性(强,非原子)IBUIImageView*imageView;
@结束
关于故事板中的连接,一切都应该是好的,也很难在这里显示,但我将尝试解释任何方式。 UICollectionView在gallery.h文件中连接,如下所示:

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

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


@end
@界面库:UIViewController
@属性(强,非原子)IBUICollectionView*collectionView;
@结束
故事板中的集合视图单元具有自定义类“customCell”和标识符“customCell”

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

在情节提要中,有时在单元格中,xCode将图像帧设置为(0,0,0,0)。它让我疯狂了很多次^^

编辑: 要创建自定义集合单元格,请创建空的XIB文件。 在其中添加一个collectionViewCell,其中包含所有您想要的内容。 与您的.h文件建立连接

在viewController->viewDidLoad方法中添加

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

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

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

在情节提要中,有时在单元格中,xCode将图像帧设置为(0,0,0,0)。它让我疯狂了很多次^^

编辑: 要创建自定义集合单元格,请创建空的XIB文件。 在其中添加一个collectionViewCell,其中包含所有您想要的内容。 与您的.h文件建立连接

在viewController->viewDidLoad方法中添加

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

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

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

我也有类似的问题。使用情节提要时,我无法在uicollection视图单元格内显示图像视图。我刚刚删除了CollectionViewController的ViewDidLoad中的以下代码:

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

现在图像视图显示在单元格内。

我遇到了类似的问题。使用情节提要时,我无法在uicollection视图单元格内显示图像视图。我刚刚删除了CollectionViewController的ViewDidLoad中的以下代码:

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

现在图像视图显示在单元格内。

谢谢您的输入,但我检查了情节提要,并且值是正确的。确定:)您是否检查了名称,如果图像是“骷髅”而不是“骷髅”?是的,我已经三次检查了所有输入错误,我坐在这里已经8个小时了,但似乎找不到问题所在…请使用cell.imageView.backgroundColor=UIColor.redColor以确保显示imageView。您是在单独的XIB中还是在主故事板中创建单元的?嗯,似乎根本没有显示imageView,没有红色方块。我会尽量朝那个方向看,也许我能找到什么。我使用了UICollectionView创建的单元格,我将其添加到主情节提要中的ViewController中。感谢您的输入,但我检查了情节提要,并且值是正确的。确定:)您是否检查了名称,如果图像是“骷髅”而不是“骷髅”?是的,我已经三次检查了所有输入错误,我坐在这里已经8个小时了,但似乎找不到问题所在…请使用cell.imageView.backgroundColor=UIColor.redColor以确保显示imageView。您是在单独的XIB中还是在主故事板中创建单元的?嗯,似乎根本没有显示imageView,没有红色方块。我会尽量朝那个方向看,也许我能找到什么。我使用了由UICollectionView创建的单元格,该单元格已添加到主情节提要中的ViewController。在cellForItem方法中添加断点或日志语句,并检查image view属性的值。在cellForItem方法中添加断点或日志语句,并检查image view属性的值。