UICollectionView单元格与图像,点击更改背景

我有一个UICollectionView Custom CollectionView Cells 。 每个细胞上都有一个图像,与整个细胞一样大。 现在我想在用户触摸单元格时突出显示单元格。 首先,我试着用下面的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]; } 

但没有任何happend.After我从单元格中删除图像,它完美的工作。 但与图像没有任何反应! 我还可以做些什么?

如果你有一个CustomCell,你必须有一个CustomCell.m(实现文件)。 在这个文件中join这个,对我来说是简单的方法:

 -(void)setHighlighted:(BOOL)highlighted { if (highlighted) { self.layer.opacity = 0.6; // Here what do you want. } else{ self.layer.opacity = 1.0; // Here all change need go back } } 

在这里,您将图像放在整个单元格上,所以单元格位于图像后面,因为图像而看不到单元格背景。 它更好地采取两个图像之一是为突出显示的图像和另一个正常。 删除这一行

 cell.contentView.backgroundColor = [UIColor redColor]; 

设置突出显示的图像如下:

 - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath]; UIImage *image11 = [UIImage imageNamed:@"your_highlighted_image"]; CGRect starFrame1 = CGRectMake(0, 0, 350, 50); UIImageView *starImage2 = [[UIImageView alloc] initWithFrame:starFrame1]; starImage2.image= image11; //set this image as cell background [cell setBackgroundView:starImage2]; } 

设置正常的图像如下:

 - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; UIImage *image11 = [UIImage imageNamed:@"your_normal_image"]; CGRect starFrame1 = CGRectMake(0, 0, 350, 50); UIImageView *starImage2 = [[UIImageView alloc] initWithFrame:starFrame1]; starImage2.image= image11; //set this image as cell background [cell setBackgroundView:starImage2]; } 

像这样尝试

 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *selectedCell = [collectionView cellForItemAtIndexPath:indexPath]; selectedCell.contentView.backgroundColor = nil; [selectedCell.contentView.layer setBorderColor:[UIColor redColor].CGColor]; [selectedCell.contentView.layer setBorderWidth:3.0f]; } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *deselectedCell = [collectionView cellForItemAtIndexPath:indexPath]; deselectedCell.contentView.backgroundColor = nil; [deselectedCell.contentView.layer setBorderColor:[UIColor clearColor].CGColor]; [deselectedCell.contentView.layer setBorderWidth:3.0f]; } 

Swift版本的接受答案

 class MyCollectionViewCell: UICollectionViewCell { override var highlighted: Bool { didSet { if (highlighted) { self.layer.opacity = 0.6; } else { self.layer.opacity = 1.0; } } } } 

资源

我发布这个答案是为了别人的利益,但我并不完全满意。 以下是我在初步观察中遇到的两个问题:

  1. 调暗是突然的,不像UIButton水龙头那样animation。
  2. 第一列中的button根本不会改变外观。 也许这是我的应用程序中的一些错误,与这个实际的方法无关。 请在评论中确认或否认。