2010-05-19 9 views
5

UIButton, UIImage 및 두 개의 UILabels이있는 맞춤 UITableViewCell 클래스를 만들었습니다. 단추와 이미지는 서로 겹쳐서 표시되며 한 번에 하나만 표시됩니다. 예상되는 동작은 버튼을 터치하면 버튼이 사라지고 이미지가 표시됩니다. UITableViewCells은 재사용하도록 설정되어 있습니다.맞춤형 UITableViewCell은 setNeedsDisplay에서 다시 그리지 않습니다.

생성자 :

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 
     unheartButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 

     unheartButton.backgroundColor = [UIColor clearColor]; 
     unheartButton.frame = CGRectMake(10, 13, 20, 18); 

     [unheartButton addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 
     [unheartButton setBackgroundImage:[UIImage imageNamed:@"redheart.png"] forState:UIControlStateNormal]; 

     imageView = [[UIImageView alloc] init]; 
     imageView.frame = CGRectMake(12, 13, 16, 16); 

     NSMutableArray *array = [[NSMutableArray alloc] init]; 

     for (int ndx = 1; ndx < 13; ndx++) { 
      [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"icon-loading-%d (dragged).tiff", ndx]]]; 
     } 

     imageView.animationImages = array; 
     imageView.animationDuration = 1; 

     [array release]; 

     [self.contentView addSubview:imageView]; 
     [self.contentView addSubview:unheartButton]; 

     return self; 
    } 
} 

    - (void) setModel:(MyModel *)myModel { 
     model = myModel; 

     if (model.hideImage) { 
       imageView.hidden = YES; 
       unheartButton.hidden = NO; 
     else { 
       imageView.hidden = NO; 
       unheartButton.hidden = YES; 
     } 
    } 

버튼 클릭 :

- (IBAction) onButtonClick: (id) sender { 
    model.hideImage = NO; 

    unheartButton.hidden = YES; 
    imageView.hidden = NO; 
    [imageView startAnimating]; 

    [self setNeedsDisplay]; 
    [self.contentView setNeedsDisplay]; 
    [self.unheartButton setNeedsDisplay]; 
    [self.imageView setNeedsDisplay]; 
} 

내가 다에 setNeedsDisplay를 호출 합니다만, 아무것도 일어날 것 같다 여기 내 코드입니다. 화면을 스크롤하여 백업하면 버튼이 숨겨 짐과 동시에로드 아이콘이 표시되지만 스크롤 후에 만 ​​발생합니다. 세포를 다시 칠하기 위해 내가 무엇을해야하는지 잘 모르겠습니다.

답변

2

UITableView는 스크롤링 등을 지원하기 위해 멋진 캐싱을 수행합니다. 사용자 지정보기를 사용할 때 특정 셀을 새로 고치는 것과 비슷한 문제가있었습니다.

reloadRowsAtIndexPaths : withRowAnimation :을 사용하면 테이블을 해당 행만큼 다시로드 할 수 있습니다. 자세한 내용은이 게시물을 참조하십시오. Why won't my UITableViewCell deselect and update its text?

+0

내 사용자 지정 UItableViewCell에서 NSIndexPath를 추가하고이를 셀에 할당 한 다음 호출하는 셀을 다시 그립니다. [selfArrow arrayWithObject : currentCell.indexPath] withRowAnimation : UITableViewRowAnimationNone]; –

관련 문제