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 に答える