1

I managed to set the cell background color when it is tapped on with this code:

- (void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:   (NSIndexPath *)indexPath
{
    NSLog(@"highlighted cell at index path %ld", (long)indexPath.row);

    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor blueColor];

}

but then I have 2 problems

1: I show 6 cells in a view, but then I slide to show the other cells, some of the cells are also highlighted with the blue background

2: After I select cell A and have the background color changed to blue, when I select B, the background color in A does not changed back to white, how could I change it back?

Thanks!

rmaddy
  • 298,130
  • 40
  • 468
  • 517
dan
  • 731
  • 2
  • 12
  • 24

2 Answers2

0

You need to reset the cell properly in your collectionView:cellForItemAtIndexPath: method.

if (/* cell is highlighted */) {
    cell.contentView.backgroundColor = [UIColor blueColor];
} else {
    cell.contentView.backgroundColor = [UIColor whiteColor]; // use whatever is appropriate
}

This of course requires that you keep track of which cells should be highlighted.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
  • erm, how do I keep track of that? – dan Apr 25 '14 at 03:43
  • also, if I select cell B, cell A still remains blue. I will need to slide it off the view to make it back to white background. – dan Apr 25 '14 at 03:44
  • How you keep track depends on whether you support only a single highlighted cell or multiple. If single, you can add an ivar that keeps track of some key related to the cell. If multiple, then you should add an additional value for each cell in your data source. – rmaddy Apr 25 '14 at 03:46
  • I am doing single highlight only. I have 18 items. So I want to let user to select one item at a time. They may tap on A and A will have blue background, but then if they tap on C, A should change back to white background while C is blue background. – dan Apr 25 '14 at 03:54
  • In the `didHighlightItemAtIndexPath` you update the currently highlighted cell's background back to white. Then you update the current cell to be blue. Then you set an ivar that indicates which cell is currently highlighted. This ivar is used in the `if` statement I posted in my answer so the proper cell is highlighted as the collection view is scrolled. – rmaddy Apr 25 '14 at 03:56
0

If you only need to have the cell highlighted to the blue background while your finger is touching the cell, you can change the color back to white in the didUnhighlightItemAtIndexPath.

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"unhighlighted cell at index path %ld", (long)indexPath.row);

    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
}

This makes it so that other cells do not get recycled with the same background color (problem 1) and cell A immediately returns to the default color after lifting your finger before tapping cell B (problem 2).

See this question and its answers for more details.

Community
  • 1
  • 1
Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198