如何更改Custom UICollectionViewCell的背景颜色?

问题描述 投票:0回答:1

我想在didSelectItemAtIndexPath中更改自定义集合视图单元格的背景颜色。这是我的实施。

- (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);
}

这是我对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

但背景颜色没有改变。当我检查样品溶液时,它们只是改变了UICollectionViewCell的背景颜色。不是自定义的。我只是想知道我能在CustomAnnotationView那样做吗?

ios objective-c uicollectionviewcell
1个回答
1
投票

它应该是

cell.contentView.backgroundColor = ColorFromRGB(COLOR_GREEN);

但是,您应该检查如何在故事板中设计RadioCollectionViewCell单元格。因为如果该单元格的设计者在contentView上添加了另一个UIView,那么您需要获得对该视图的引用并改变该视图的背景。

© www.soinside.com 2019 - 2024. All rights reserved.