2016-09-14 39 views
0

私は選択時にコレクションビューセルに画像を追加する必要があるアプリケーションで作業しています。コレクションビューのセルは円形で色が塗りつぶされているので、セルの選択時には、ティックのイメージがその色になるはずです。iOSで背景色を持つコレクションビューセルに画像を追加

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {   
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 

NSArray *hexaColor= _advantageModelObj.productDetail.carColorDetails; 
    CarColorDetailObject *carColor= [hexaColor objectAtIndex:indexPath.row]; 

NSString *hexCode= carColor.carColor.hexCode;        
cell.layer.masksToBounds=TRUE;  
cell.backgroundColor=[self colorWithHexString:hexCode];  
cell.layer.cornerRadius= (roundf(cell.frame.size.width/2));   

cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"colorselected.png"]]; 

    return cell;   
} 
+0

あなたが試したコードを私たちに教えてください。 –

+0

また、あなたの必要性を理解するためにここにいくつかのスクリーンショットを表示します。 –

+0

私は円のセルのコレクションビューを持っているので、私はそのセルをクリックすると、その選択を示す画像がセルに追加される必要がありますそのため背景色も見えます –

答えて

0
  1. グローバル変数

int型clickedIndexください。 viewDidLoadで

2 clickedIndexに割り当てる

clickedIndex=-1; 
collectionView didSelectItemAtIndexPathで
  • clickedIndex=(int)indexPath.row; 
    [collectionView reloadData]; 
    
  • collectionView cellForItemAtIndexPathにいくつかの行を追加
    if(clickedIndex==indexPath.row){ 
    
    
    UIImageView* imageView=[[UIImageView alloc]init]; 
    [imageView setFrame:CGRectMake(10, 0, cell.contentView.size.width, cell.contentView.size.height)]; 
    imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@“tickImage”,]]; 
    imageView.alpha=0.5; 
    
    [cell.contentView addSubview: imageView]; 
    
    
    
        } 
    
    関連する問題