2017-12-21 6 views
0

didSelectItemAtIndexPath에서 내 사용자 지정 컬렉션보기 셀의 배경색을 변경하고 싶습니다. 내 구현은 다음과 같습니다.사용자 지정 CollectionViewCell의 배경색을 변경하는 방법?

- (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에서 할 수 있는지 알고 싶습니까?

+0

코드를 추가하십시오. –

답변

1

그것은 당신이 스토리 보드 자체에 RadioCollectionViewCell 셀을 설계하는 방법을 확인해야합니다, 그러나

cell.contentView.backgroundColor = ColorFromRGB(COLOR_GREEN); 

해야한다. 그 셀의 디자이너가 contentView 위에 또 다른 UIView를 추가 한 경우 해당 뷰에 대한 참조를 가져 와서 대신 해당 뷰의 배경을 변경해야합니다.

+0

답장을 보내 주셔서 감사합니다. 실제로 RadioCollectionViewCell과 동일한 너비와 높이의 레이블을 추가했습니다. 셀의 배경색 대신 해당 레이블의 색을 변경하려고했습니다. 그러나 그것은 효과가 없었습니다. 왜 그랬는지 몰라. –

+0

레이블의 색상을 변경하려면 레이블의 iboutlet을 가져 와서 정상적으로 색상을 설정해야합니다. – GeneCode

+0

예, 물론입니다. 나는 그것을 시도했다. 그러나 그것은 효과가 없었습니다. 유선 .. –

관련 문제