Mitesh Dobareeya
Mitesh Dobareeya

Reputation: 1020

How to set multiple images in collection view cell?

I have a list of album images that i displayed in collection view. The problem is when i select image from collection view i have to set another image on cell that is CheckMark.png image,which shows image selected check mark. please help me to solve this.

I use this code to set image but it was not working.

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

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

Upvotes: 0

Views: 2208

Answers (3)

Vvk
Vvk

Reputation: 4043

first you have to add BOOL in AlbumImageCell.h same like below code...

@property (assign, nonatomic) BOOL isCellSelected;

Then Update your code in both cellForItemAtIndexPath and didSelectItemAtIndexPath both method as per your need

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

another way is store selected cell's indexpaths in array from didSelectItemAtIndexPath and check in cellForItemAtIndexPath method. but Bool is very easy insted of this. Hope it helps you.

Upvotes: 0

Dheeraj Kumar
Dheeraj Kumar

Reputation: 500

In your data source add property bool type for each modal. Then make bool false/true at time of check click and reload data.

Upvotes: 1

Sreekanth
Sreekanth

Reputation: 549

This is my method to solve this task ,You need to store boolean value No in an another array name as like checkMarkArray same as size of image array.Then you check the checkMarkArray boolean value ,if the boolean value is true set image to the imageview in the collectionView:cellForItemAtIndexPath: like this

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

And in the collectionView:didSelectItemAtIndexPath: change value of object at index to boolean True in the checkMarkArray.

Upvotes: 0

Related Questions