点击集合查看单元格更改图像

Posted

技术标签:

【中文标题】点击集合查看单元格更改图像【英文标题】:tap collection view cell change image 【发布时间】:2013-01-10 20:03:55 【问题描述】:

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

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

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)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];

【问题讨论】:

【参考方案1】:

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

//CustomCell.m
- (id)initWithFrame:(CGRect)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)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    CustomCell *cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell tapToChangeBackGround];

【讨论】:

以上是关于点击集合查看单元格更改图像的主要内容,如果未能解决你的问题,请参考以下文章

如何更改集合视图单元格中的图像视图

图像的集合视图和弹出窗口上的单元格显示图像,也可以滚动查看其他图像

滚动时collectionview更改单元格背景图像

根据所选的 indexpath.row 更改 UICollectionView 单元格图像视图

集合视图在第一个索引上设置单选按钮图像并选择更改

在被点击的单元格的索引处更改数据源