I have a UICollectionView
with Custom CollectionView Cells
. On each Cell there is a Image on it, which is as big as the whole Cell. Now i want to highlight the Cell when the User touches the Cell. First i tried it with the following delegate Methods
:
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor redColor];
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor clearColor];
}
But nothing happend.After i deleted the Images from the Cells it worked perfectly. But with the Images nothing happens! What else can I do?
Here you place image on whole over the cell, so the cell is behind the image, you can not see the cell background because of image. Its better to take two image one is for highlighted image and another for normal. remove this line
set the highlighted image as follows:
set the normal image as follows:
Swift version of the accepted answer
Source
I am posting this answer for the benefit of others, but I am not completely satisfied with it. Here are two problems I have come across in my initial observation:
UIButton
tap.If you have a CustomCell, you must have a CustomCell.m (implementation file). In this file add this, to me is the easy way:
Try like this