views:

278

answers:

1

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.

A: 

Fixed, The problem was in my model class!

Andrew Portner
Well, what *was* the problem?
Peter Hosey
I'm having this exact problem and it would be awesome if you could give some more details as to the issue and the fix.
taber
Oops - in my case I was trying to access a view's tag in my cell that didn't exist!
taber