views:

35

answers:

1

I have created a custom UITableViewCell where I add a bunch of buttons loaded dynamically to its contentView. These display properly.

When the data which controls what buttons are loaded change and I call reloadData I can see that prepareForReuse is called:

- (void) prepareForReuse {
    NSLog(@"prep for reuse");
    [self clearButtons];
}

- (void) clearButtons {
    NSLog(@"clearButtons called");
    self.buttons;
    for (UIView* v in buttons) {
        NSLog(@"clearing a button.");
        [v removeFromSuperview];
        [v dealloc];
    }
    buttons = [[NSMutableArray alloc] init];
}

The buttons are not removed from the superview though, the "clearing a button." message is never logged.

I then added an explicit call to clearButtons in tableView:cellForRowAtIndexPath:

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

[cell clearButtons];

And this time I can see that "clearing a button." is logged and everything displays correctly.

What gives? Why aren't the buttons removed when called from prepareForReuse?

+1  A: 

It might not related, but there are few things you should fix in createButtons:

  1. Use [v release] instead of [v dealloc]
  2. Release the buttons as well before init new one.

I would also ask that if you remove everything but NSLog, does it works?

tia