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

时间:2014-11-28 10:55:23

标签: ios objective-c uicollectionview uicollectionviewcell

我有UICollectionView Custom 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];
}

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

4 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:4)

此处您将图像放在整个单元格上,因此单元格位于图像后面,因图像而无法看到单元格背景。最好拍摄两张图像一张用于高亮显示图像,另一张用于普通图像。 删除此行

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];
}

答案 2 :(得分:2)

试试这个

- (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];
}

答案 3 :(得分:0)

接受答案的Swift版本

class MyCollectionViewCell: UICollectionViewCell {

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

Source

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

  1. 调光是突然的,不像UIButton点击那样动画。
  2. 第一列中的按钮根本不会改变外观。也许这是我的应用程序中的一些错误,与此实际方法无关。请在评论中确认或否认。