2

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

突出显示(在下面效果很好)之后,我希望它在再次触摸时回到旧图像。谢谢你。在 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];
}
4

1 回答 1

4

创建 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];
}
于 2013-01-12T05:06:45.613 回答