2013-10-24 5 views
5

私はiOSでアプリケーションを構築しています。私のCollectionViewのセルをタッチすると、通常のボタンとほとんど同じように強調します。 didSelectItemAtIndexPath:(NSIndexPath *)indexPathメソッドでこれをどのように達成できますか?CollectionViewのセルをハイライトします

おかげ

+0

そのセルの背景色を変更します。 – user1673099

答えて

7

このような何かを試してみてください:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    ..... 
    if (cell.selected) { 
     cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; // highlight selection 
    } 
    else 
    { 
     cell.backgroundColor = [UIColor clearColor]; // Default color 
    } 
    return cell; 
} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; //  //cell.lblImgTitle.text = @"xxx"; 
} 

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor clearColor]; 
} 
+0

didHighlightItemAtIndexPathメソッドとdidUnhighlightItemAtIndexPathメソッドのセル内の画像のアルファを変更して解決しました。とにかく、私は正しい答えとしてあなたの答えを設定します。ありがとう。 –

+1

答えであなたの問題をどのように解決したのですか?それは他の人に役立つだろう... diogo-appdev –

0

はあなたがセルクラスをサブクラス化した場合、この

cell.selectedBackgroundView.backgroundColor = [UIColor greenColor]; 
5

を試してみてください、あなたの.mファイルでこれを入れ

- (void)setSelected:(BOOL)selected 
{ 
    if(selected) 
    { 
     self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5]; 
    } 
    else 
    { 
     self.backgroundColor = [UIColor whiteColor]; 
    } 
} 
0

変更できない理由カカオポッドによる背景色?私は新しいユーザ定義コレクションビューのセルクラス `

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
     CHSMenuControlCell *cell = (CHSMenuControlCell*)[collectionView cellForItemAtIndexPath:indexPath]; 
     cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; //  //cell.lblImgTitle.text = @"xxx"; 
    } 

    - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    CHSMenuControlCell *cell = (CHSMenuControlCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor clearColor]; 
} 
関連する問題