1

I have a collectionView and each cell contains an image. Now I want to highlight the image to become highlighted like the iPhone app icon when you select it.

I had try this code but it doesn't work,it just highlighted the background.

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
    {  Cell* cell = (Cell*)[collectionView cellForItemAtIndexPath:indexPath];
    cell.img.tintColor = [UIColor colorWithWhite:0 alpha:0.5 ];
    return YES;
}
Akhilrajtr
  • 5,122
  • 3
  • 17
  • 30
mustafa
  • 349
  • 4
  • 12

2 Answers2

1

A better approach is to use didSelect and didDeselect delegates. When the user selects the cell, grab the imageview in collectionView cell and change any property as you wish.

Example code.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

UICollectionViewCell *selectedCell = [collectionView cellForItemAtIndexPath:indexPath];

UIImageView *imageView = (UIImageView *)[selectedCell viewWithTag:101];
[imageView setImage:[UIImage imageNamed:@"whatever-image-you-want"]];

}

If the user selects another cell, in other words deSelects the previous cell you can change the cell using. didDeselectItemAtIndexPath delegate

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {


UICollectionViewCell *deSelectedCell = [collectionView cellForItemAtIndexPath:indexPath];

UIImageView *imageView = (UIImageView *)[deSelectedCell viewWithTag:101];
imageView.image = nil;

}
0

Do a reloadItemsAtIndexPaths: there instead, then in cellForItemAtIndexPath, check if [[collectionView indexPathsForSelectedItems] containsObject: indexPath] If true, then just change the cell's attributes there.

I hope it will help you. Thanks.