2010-05-19 54 views
5

我創建了一個定製的UITableViewCell類,其中有UIButtonUIImage和兩個UILabels。按鈕和圖像疊加在一起,每次只顯示一個。預期的行爲是您觸摸按鈕,按鈕消失,並顯示圖像。 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並將其分配給單元格,然後重繪單元格我調用:[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:currentCell.indexPath] withRowAnimation: UITableViewRowAnimationNone]; – 2016-04-26 06:45:42