OStack程序员社区-中国程序员成长平台

标题: ios - 点击集合查看单元格更改图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 00:24
标题: ios - 点击集合查看单元格更改图像

我有一个集合单元格,想在触摸时更改图像,然后再返回,我应该如何构造它?

突出显示后(在下面效果很好),我希望它在再次触摸时返回到旧图像。谢谢你。在 viewWillDissapear 中,我想知道突出显示了哪些单元格。

-(void)collectionViewUICollectionView *)collectionView didSelectItemAtIndexPathNSIndexPath *)indexPath{

    UIImage *backGround =[UIImage imageNamed"IconHighlight.png"];
    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, backGround.size.width, backGround.size.height)];
    av.backgroundColor = [UIColor clearColor];
    av.opaque = NO;
    av.image = backGround;

    [[collectionView cellForItemAtIndexPath:indexPath] setBackgroundView:av];
    [[collectionView cellForItemAtIndexPath:indexPath].backgroundView setTag:1];
}



Best Answer-推荐答案


创建 CustomCell - UICollectionViewCell 的子类。自定义 init 如下

//CustomCell.m
- (id)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        backgroundImageView.image = [UIImage imageNamed"background.png"];
        highlightImageView.image = [UIImage imageNamed"highlight.png"];
        self.backgroundView = backgroundImageView;
        _isHighlight = -1;
    }
    return self;
}

-(void)tapToChangeBackGround{

    self.isHighlight = -self.isHighlight;

    if (self.isHighlight==1) {
        self.backgroundView = highlightImageView;
    }
    else{
        self.backgroundView = backgroundImageView;
    }
}

//didSelect
-(void)collectionViewUICollectionView *)collectionView didSelectItemAtIndexPathNSIndexPath *)indexPath{
    CustomCell *cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell tapToChangeBackGround];
}

关于ios - 点击集合查看单元格更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14266312/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4