如何在集合视图单元格中设置多个图像?

时间:2016-02-15 08:26:46

标签: ios objective-c uicollectionview ios9

我有一个我在收藏夹视图中显示的专辑图片列表。 问题是当我从集合视图中选择图像时,我必须在单元格上设置另一个图像,即CheckMark.png图像,它显示图像选中的复选标记。 请帮我解决这个问题。

我使用此代码来设置图像,但它无效。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

     AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];
    [cell.albumImage setImage:[UIImage imageNamed:@"CheckMark.png"]];
 }

3 个答案:

答案 0 :(得分:1)

在您的数据源中为每个模态添加属性bool类型。然后在检查点击并重新加载数据时使bool为false / true。

答案 1 :(得分:0)

这是我解决此任务的方法,你需要将布尔值存储在另一个数组名称中,就像 checkMarkArray 一样,与图像数组的大小相同。然后你检查 checkMarkArray 布尔值,如果布尔值为true,则将图像设置为collectionView:cellForItemAtIndexPath:中的imageview

 [cell.albumImage setImage:[UIImage imageNamed:@"CheckMark.png"]]

checkMarkArray 中,collectionView:didSelectItemAtIndexPath:对象对象的值更改为布尔值True。

答案 2 :(得分:0)

首先你必须在 AlbumImageCell.h 中添加BOOL,就像下面代码一样......

@property (assign, nonatomic) BOOL isCellSelected;

然后根据您的需要以cellForItemAtIndexPathdidSelectItemAtIndexPath方式更新您的代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

     AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];

    if (cell.isCellSelected) {
        //add chackmark image here
        [cell.albumImage setImage:[UIImage imageNamed:@"CheckMark.png"]];
    }else {
        //add unchak image here
        [cell.albumImage setImage:[UIImage imageNamed:@"UNCheckMark.png"]];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];

    if (cell.isCellSelected) {
        cell.isCellSelected = FALSE;
        //set unchacked image to cell
        [cell.albumImage setImage:[UIImage imageNamed:@"UNCheckMark.png"]];
    }else {
        cell.isCellSelected = TRUE;
        //set checked image to cell here like
        [cell.albumImage setImage:[UIImage imageNamed:@"CheckMark.png"]];
    }
}

另一种方法是从didSelectItemAtIndexPath在数组中存储选定单元格的索引路径,并检入cellForItemAtIndexPath方法。但Bool很容易对此感到厌烦。 希望它能帮到你。