0

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?

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Nyein Ei Ei Tun
  • 168
  • 2
  • 13

1 Answers1

1

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.

GeneCode
  • 7,545
  • 8
  • 50
  • 85
  • Thanks for the reply. Actually I have added a label with the equal width and height of that RadioCollectionViewCell. I tried to change the color of that label instead of cell's background color. But it did not work. I dun know why. – Nyein Ei Ei Tun Dec 21 '17 at 04:11
  • If you want to change color of label then u need to get the label’s iboutlet and set the color normally – GeneCode Dec 21 '17 at 04:13
  • Show the code how u change label color. and also post the header of your RadioCollectionViewCell. – GeneCode Dec 21 '17 at 04:18
  • I have updated the questions for more code. Please check. – Nyein Ei Ei Tun Dec 21 '17 at 04:36
  • I updated my code according to this answer [link](https://stackoverflow.com/questions/23402263/difference-between-dequeuereusablecellwithreuseidentifier-and-cellforitematinde). And it works now. I updated "cellforItemAtIndexPath" instead of using "dequeueReusableCellWithReuseIdentifier". Thanks for ur help. – Nyein Ei Ei Tun Dec 21 '17 at 05:51