2010-05-19 24 views
5

Tôi đã tạo một lớp tùy chỉnh UITableViewCell với một UIButton, một UIImage và hai UILabels. Nút và hình ảnh được chồng lên nhau và chỉ một hình được hiển thị cùng một lúc. Hành vi mong đợi là bạn chạm vào nút, nút biến mất và hiển thị hình ảnh. Các UITableViewCells được thiết lập để được tái sử dụng. Dưới đây là mã của tôi:Tùy chỉnh UITableViewCell sẽ không vẽ lại trên setNeedsDisplay

vị thi công:

- (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; 
     } 
    } 

Nút bấm:

- (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]; 
} 

Tôi gọi setNeedsDisplay về mọi thứ, nhưng không có gì có thể xảy ra. Nếu tôi di chuyển khỏi màn hình và sao lưu, nút này bị ẩn và bây giờ biểu tượng tải được hiển thị, nhưng điều này chỉ xảy ra sau khi cuộn. Tôi không chắc mình cần phải làm gì để lấy lại tế bào.

Trả lời

2

UITableView thực hiện một số bộ nhớ đệm ưa thích để hỗ trợ cuộn và tương tự; Tôi đã có các vấn đề tương tự với việc làm mới một ô cụ thể khi tôi sử dụng chế độ xem tùy chỉnh.

Bạn có thể sử dụng reloadRowsAtIndexPaths: withRowAnimation: để làm cho bảng tải lại chỉ hàng đó. Xem bài đăng này để biết thêm thông tin: Why won't my UITableViewCell deselect and update its text?

+0

Trong UItableViewCell tùy chỉnh của tôi, tôi thêm NSIndexPath và gán nó vào ô, sau đó vẽ lại ô mà tôi gọi: [self.tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject: currentCell.indexPath] withRowAnimation: UITableViewRowAnimationNone]; –

Các vấn đề liên quan