Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableViewCell won't redraw on setNeedsDisplay

I created a custom UITableViewCell class with a UIButton, a UIImage, and two UILabels. The button and the image are overlayed on top of each other, and only one is displayed at a time. The expected behavior is you touch on the button, the button disappears, and it displays the image. The UITableViewCells are set to be reused. Here's my code:

Constructor:

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

Button click:

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

I'm calling setNeedsDisplay on everything, but nothing seems to happen. If I scroll off the screen and back up, the button is hidden and now the loading icon is shown, but this only happens after a scroll. I'm not sure what I need to do to get the cell to repaint.

like image 764
Andrew Portner Avatar asked Nov 05 '22 13:11

Andrew Portner


1 Answers

The UITableView does some fancy caching to support scrolling and the like; I've had similar issues with refreshing a specific cell when I use custom views.

You can use reloadRowsAtIndexPaths:withRowAnimation: to make the table reload just that row. See this post for more information: Why won't my UITableViewCell deselect and update its text?

like image 180
russbishop Avatar answered Nov 12 '22 09:11

russbishop