6

I'm building an app in iOS and I want the Cells in my CollectionView to highlight when touched, pretty much like the normal buttons. How can I achieve this in the didSelectItemAtIndexPath:(NSIndexPath *)indexPath method?

Jack
  • 13,571
  • 6
  • 76
  • 98
diogo.appDev
  • 1,595
  • 5
  • 16
  • 30

4 Answers4

7

Try something like this:

- (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];
}
acoustic
  • 4,557
  • 3
  • 19
  • 18
Calin Chitu
  • 1,099
  • 7
  • 13
  • I solved it by changing the Alpha of the picture in the cell in the didHighlightItemAtIndexPath and didUnhighlightItemAtIndexPath methods. Anyway, I'll set your answer as the right answer. Thanks. – diogo.appDev Oct 24 '13 at 10:37
  • 1
    why don't you post how did you solve your issue in the answer? It will be helpful for others ...diogo-appdev – Narasimha Nallamsetty Mar 26 '15 at 18:16
5

If you subclassed the cell class, put this in your .m file

- (void)setSelected:(BOOL)selected
{
    if(selected)
    {
        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    }
    else
    {
        self.backgroundColor = [UIColor whiteColor];
    }
}
BaSha
  • 2,356
  • 3
  • 21
  • 38
CyberMew
  • 1,159
  • 1
  • 16
  • 33
0

try this

cell.selectedBackgroundView.backgroundColor = [UIColor greenColor];
Vaisakh
  • 2,919
  • 4
  • 26
  • 44
0

why can not change the background color through the cocoa pods?I new a user-defined collectionView cell class`

   - (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];
}
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
lily
  • 11
  • 3