使用NSArray中的UIImageViews填充UICollectionView单元格

时间:2014-03-20 15:54:51

标签: ios iphone uiimageview nsarray uicollectionview

我有一个嵌入在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创建了出口。

标签工作得很好,但图像不是,而且它已经崩溃了。

对此的任何帮助都会非常好,而且非常有帮助!

1 个答案:

答案 0 :(得分:1)

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

它的名称ReddishBlack.png不是ReddishBack.png吗?