Nyein Ei Ei Tun
Nyein Ei Ei Tun

Reputation: 168

How to change background color of Custom CollectionViewCell?

I would like to change the background color of my custom collection view cell in the didSelectItemAtIndexPath. Here is my implementation.

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

    RadioCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RadioCollectionViewCell" forIndexPath:indexPath];
    [cell setData:self.contentModel andIndexPath:indexPath];
    cell.lblChoice.backgroundColor = ColorFromRGB(COLOR_GREEN);
}

Here is my implementation for RadioCollectionViewCell.h

#import <UIKit/UIKit.h>

@interface RadioCollectionViewCell : UICollectionViewCell
    @property (weak, nonatomic) IBOutlet UILabel *lblChoice;

    - (void)setData:(ContentModel *)contentModel andIndexPath:(NSIndexPath *)indexPath;
    + (RadioCollectionViewCell *)loadFromNib;
@end

But the background color did not change. When I check the sample solutions, they are just changing the background color of UICollectionViewCell. Not the custom one. I just want to know that can I do that in the CustomAnnotationView?

Upvotes: 0

Views: 90

Answers (1)

GeneCode
GeneCode

Reputation: 7588

It should be

cell.contentView.backgroundColor = ColorFromRGB(COLOR_GREEN);

However, you should check how you are designing the RadioCollectionViewCell cell in storyboard itself. Because if the designer of that cell added another UIView on top of the contentView, then you need to get a reference to that view and change the background of that view instead.

Upvotes: 1

Related Questions