Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView Cell with Image, change Background with click

I have a UICollectionView with Custom CollectionView Cells. On each Cell there is a Image on it, which is as big as the whole Cell. Now i want to highlight the Cell when the User touches the Cell. First i tried it with the following delegate Methods:

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor redColor];

}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor clearColor];
}

But nothing happend.After i deleted the Images from the Cells it worked perfectly. But with the Images nothing happens! What else can I do?

like image 855
Davis Avatar asked Nov 25 '25 20:11

Davis


2 Answers

If you have a CustomCell, you must have a CustomCell.m (implementation file). In this file add this, to me is the easy way:

-(void)setHighlighted:(BOOL)highlighted
{
    if (highlighted)
    {
        self.layer.opacity = 0.6;
        // Here what do you want.
    }
    else{
        self.layer.opacity = 1.0;
        // Here all change need go back
    }
}
like image 128
Onik IV Avatar answered Nov 28 '25 12:11

Onik IV


Here you place image on whole over the cell, so the cell is behind the image, you can not see the cell background because of image. Its better to take two image one is for highlighted image and another for normal. remove this line

cell.contentView.backgroundColor = [UIColor redColor];

set the highlighted image as follows:

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath];
UIImage *image11 = [UIImage imageNamed:@"your_highlighted_image"];
    CGRect starFrame1 = CGRectMake(0, 0, 350, 50);  
    UIImageView *starImage2 = [[UIImageView alloc] initWithFrame:starFrame1];   
    starImage2.image= image11; 
//set this image as cell background 
    [cell setBackgroundView:starImage2];

}

set the normal image as follows:

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
UIImage *image11 = [UIImage imageNamed:@"your_normal_image"];
    CGRect starFrame1 = CGRectMake(0, 0, 350, 50);  
    UIImageView *starImage2 = [[UIImageView alloc] initWithFrame:starFrame1];   
    starImage2.image= image11; 
//set this image as cell background 
    [cell setBackgroundView:starImage2];
}
like image 38
Sabrina Tuli Avatar answered Nov 28 '25 13:11

Sabrina Tuli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!