2013-01-10 96 views
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

創建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]; 
} 
相關問題