Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在NSArray中使用UIImageView填充UICollectionView单元格_Ios_Iphone_Uiimageview_Nsarray_Uicollectionview - Fatal编程技术网

Ios 在NSArray中使用UIImageView填充UICollectionView单元格

Ios 在NSArray中使用UIImageView填充UICollectionView单元格,ios,iphone,uiimageview,nsarray,uicollectionview,Ios,Iphone,Uiimageview,Nsarray,Uicollectionview,我在UIViewController中嵌入了一个UICollectionView(CollectionView),在各个单元格中嵌入了一个UICollectionViewCell(子类-ThemeCell) 我试图使用来自ImageView的NSArray的图像填充UICollectionView中的每个单元格,但出现以下错误: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***

我在UIViewController中嵌入了一个UICollectionView(CollectionView),在各个单元格中嵌入了一个UICollectionViewCell(子类-ThemeCell)

我试图使用来自ImageView的NSArray的图像填充UICollectionView中的每个单元格,但出现以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[12]'
我为每个单元格嵌入了一个标签,效果非常好。 这是我的行动计划

在Mecell中,我有两个属性:

@property (weak, nonatomic) IBOutlet UILabel *cellLabel;
@property (weak, nonatomic) IBOutlet UIImageView *cellImages;
CollectionViewCell
中,我在.h中有以下代码:

@property (weak, nonatomic) IBOutlet UICollectionView *cView;
@property (nonatomic, strong) NSArray *themeLabels;
@property (nonatomic, strong) NSArray *themeImages;
这是电话号码

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.cView.dataSource = self;
    self.cView.delegate = self;
    self.themeLabels = [[NSArray alloc] initWithObjects:@"Default", @"Peacock", @"Purple", @"Rainbow", @"Multi Zebra", @"Green", @"Marble", @"Prosperity", @"Leopard", @"Circle", @"Slanted", @"Orange", @"Reddish", nil];
    NSLog(@"The themes are %@", self.themeLabels);

    self.themeImages = @[[UIImage imageNamed:@"Newiphonebackground.png"], [UIImage imageNamed:@"peacock.png"], [UIImage imageNamed:@"Purplepink.png"], [UIImage imageNamed:@"Rainbow.png"], [UIImage imageNamed:@"PinkZebra.png"], [UIImage imageNamed:@"Greenish.png"], [UIImage imageNamed:@"MarblePrint.png"], [UIImage imageNamed:@"Prosperity.png"], [UIImage imageNamed:@"leopard.png"], [UIImage imageNamed:@"CircleEffect.png"], [UIImage imageNamed:@"RedSlanted.png"], [UIImage imageNamed:@"Orange3.png"], [UIImage imageNamed:@"ReddishBack.png"]];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.themeLabels count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ThemeCell *themeCell = (ThemeCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Theme Cell" forIndexPath:indexPath];
    NSString *cellData = [self.themeLabels objectAtIndex:indexPath.row];
    themeCell.cellLabel.text = cellData;
    themeCell.cellImages.image = self.themeImages[indexPath.row];
    return themeCell;
}
异常断点发生在:

themeCell.cellImages.image = self.themeImages[indexPath.row];
我在storyboard中构建了CollectionView,并在其中放置了一个单元格,为Label和UIImageView创建了插座

标签工作得很好,但图像不是这样,它正在崩溃


在这方面的任何帮助都将非常有用

我认为
self.themeImages
数组中索引为12(最后一个)的元素为零。您确定图像文件名正确且文件存在吗


它的名字不是
ReddishBack.png
而不是
ReddishBack.png

非常感谢@Antonio-修复了错误,我不敢相信我错过了。但是,当CollectionView加载时,它不会在单元格中显示图像-它只是显示紫色框(这是我在故事板中将CustomCollectionViewCell设置为的颜色。如果我将其设置为“清除”,则我只会看到一个白色屏幕。NSLog(@“图像是%@”,self.themeImages);提供结果,但不提供图像名称。它提供UIImage:0x17809f540等。我缺少什么来显示图像?很抱歉,图像现在显示在每个单元格上,但每个单元格中的标签现在都消失了。它不显示,我似乎不明白为什么?因此图像可以工作并显示,但每个单元格的标签都不显示(但如果我把这些图片注释掉的话就可以了).
UIImage
s被实例化并用从文件加载的图像填充-一旦完成,每个实例与加载它的文件之间就没有进一步的关系。您得到的输出是正确的,因为这意味着:指向地址为
0x17809f540
UIImage
实例的指针。可能是图像与标签重叠-可能吗?请尝试移动顶部的标签和下方的图像。您是否设置了任何布局约束以使图像根据其大小而增长?非常感谢@Antonio-这是图像重叠。非常感谢您在这方面的帮助以及这里的出色答案。再次感谢!