带图像的UICollectionView单元格,单击更改背景 [英] UICollectionView Cell with Image, change Background with click
问题描述
我有一个 UICollectionView
,带有 Custom CollectionView Cells
。在每个Cell上都有一个Image,它与整个Cell一样大。现在我想在用户触摸Cell时突出显示Cell。首先,我尝试使用以下委托方法
:
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?
推荐答案
如果你有CustomCell,你必须有一个CustomCell.m(实现文件)。在这个文件中添加这个,对我来说很简单:
If you have a CustomCell, you must have a CustomCell.m (implementation file). In this file add this, to me is the easy way:
-(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
}
}
这篇关于带图像的UICollectionView单元格,单击更改背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!