2013-01-10 3 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

UICollectionViewCell의 CustomCell 하위 클래스를 만듭니다. 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]; 
} 
관련 문제