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

2023-12-10

我有一个UICollectionView with Custom CollectionView Cells。每个Cell上都有一个Image,它和整个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];
}

但什么也没发生。当我从单元格中删除图像后,它工作得很好。但图像什么也没有发生!我还可以做些什么?


如果您有 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
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UICollectionView 单元格与图像,单击更改背景 的相关文章

随机推荐