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?