【问题标题】:Populating UICollectionView cells with UIImageViews in an NSArray在 NSArray 中使用 UIImageViews 填充 UICollectionView 单元格
【发布时间】:2014-04-27 14:51:24
【问题描述】:

我有一个嵌入在 UIViewController 中的 UICollectionView (CollectionView) 和一个用于单个单元格的 UICollectionViewCell (子类 - ThemeCell)。

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

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[12]'

我为每个单元格嵌入了一个标签,效果很好。 这是我的行动计划。

ThemeCell,我有两个属性:

@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;

这是.m

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

我在情节提要中建立了 CollectionView,并在其中放置了一个单元格并为 Label 和 UIImageView 创建了出口。

标签工作得很好,但图像却不行,而且它正在崩溃。

在这方面的任何帮助都会非常棒并且非常有帮助!

【问题讨论】:

    标签: ios iphone uiimageview nsarray uicollectionview


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢@Antonio - 这修复了错误,我不敢相信我错过了。但是,当 CollectionView 加载时,它没有显示单元格中的图像 - 它只是显示紫色框(这是我在 storyBoard 中设置 CustomCollectionViewCell 的颜色。如果我让它“清晰”,那么我只会得到一个白屏. NSLog(@"The images are %@", self.themeImages); 给我结果,但不是图像名称。它给出 UIImage: 0x17809f540 等。我缺少什么来显示图像?
    • 抱歉图片现在显示在每个单元格上,但现在每个单元格中的标签已经消失。它没有显示,我似乎无法弄清楚为什么?所以图像可以工作并显示,但每个单元格的标签不会(但如果我注释掉图像)。
    • UIImages 被实例化并填充了从文件加载的图像 - 一旦完成,每个实例与从中加载它的文件之间没有进一步的关系。您得到的输出是正确的,因为这意味着:指向地址为 0x17809f540UIImage 实例的指针。
    • 也许图像与标签重叠 - 这可能吗?尝试移动顶部的标签和其下方的图像。您是否设置了任何布局约束以让图像根据其大小而增长?
    • 非常感谢@Antonio - 这是图像重叠。非常感谢您在此方面的帮助以及这里的出色答案。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    相关资源
    最近更新 更多