带图像的UICollectionView单元格,单击更改背景

Dav*_*vis 3 objective-c ios uicollectionview uicollectionviewcell

我有一个UICollectionViewCustom CollectionView Cells.在每个Cell上都有一个Image,它与整个Cell一样大.现在我想在用户触摸Cell时突出显示Cell.首先,我尝试了以下内容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];
}
Run Code Online (Sandbox Code Playgroud)

但没有任何事情发生.我删除了细胞中的图像,它完美地工作.但随着图像没有任何反应!我还可以做些什么?

Oni*_* IV 5

如果您有CustomCell,则必须具有CustomCell.m(实现文件).在这个文件中添加这个,对我来说是简单的方法:

-(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
    }
}
Run Code Online (Sandbox Code Playgroud)