The premise is simple. I have a UITablewView
. I have a storyboard where I configured two prototype cells, one has a Reuse Identifier of headerCell, the other's Reuse Identifier is cell. I am instantiating a custom header cell like this in my UITableViewController
:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
MyHeaderCell* headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell"];
headerCell.delegate = self;
return headerCell;
}
My headerCell has a button on it, and when that button is tapped, it calls [_delegate toggleHeaderSize]
. You can see above that the delegate property is simply the UITableViewController
. Here is the toggleHeaderSize
method:
-(void)toggleHeaderSize {
[self.tableView beginUpdates];
if (headerHeight == 100) {
headerHeight = 120;
} else {
headerHeight = 100;
}
[self.tableView endUpdates];
// [self.tableView reloadData];
}
That headerHeight variable is used in the heightForHeaderInSection
method, like this:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return headerHeight;
}
The header changes size, but it's weird. The table slides down, but the header doesn't actually grow. If I scroll the table just one pixel, the header seems to realize it needs to redraw, and it snaps to size.
I think I've read about every SO post about resizing header cells. They all seem to think the magic is with the beginUpdates/endUpdates
dance. These do seem to be necessary, but it doesn't solve the problem of redrawing.
Here's an animated GIF of my problem. EDIT: I can't post the animated GIF since I don't have 10 reputation points, but if you go to http://nspaul.imgur.com/all/ you can find it :-)
I'd love any guidance. I can post more code if someone would like. I'll even post the whole project to Github
if that helps.