按钮单击在集合视图中显示图像

时间:2016-09-15 08:25:53

标签: ios objective-c uicollectionview custom-cell

我想在我的UICollectionView中显示数组中的随机图像。我使用xib,并已创建带图像视图的自定义单元格。我希望当我单击我的按钮时,图像显示在集合视图中,但不知道如何执行此操作。我在tempArr中有2个数组我初始化图像,在arrayForImages中我按下按钮时插入图像。

这是我的代码:

- (IBAction)randomButton:(id)sender {
    NSInteger randIndex =  arc4random() % [tempArr count];
    NSInteger ind = arrayForImages.count;
    [arrayForImages insertObject:tempArr[randIndex] atIndex:ind];
    NSLog(@"array for index %@", arrayForImages);

}

这是集合

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return arrayForImages.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake(100, 100);
}

- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    cell.imageViewCell.image = [UIImage imageNamed:[arrayForImages objectAtIndex:indexPath.row]];

    return cell;
}

0 个答案:

没有答案