2013-10-24 221 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方法中單元格中的圖片的Alpha來解決此問題。無論如何,我會把你的答案設定爲正確答案。謝謝。 –

+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

爲什麼不能改變通過可可豆莢的背景顏色?我新增了一個用戶自定義的collectionView細胞分類號'

- (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]; 
} 
相關問題