Skip to content

Instantly share code, notes, and snippets.

@mteece
Created February 10, 2016 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mteece/a5e95afcd2672be20775 to your computer and use it in GitHub Desktop.
Save mteece/a5e95afcd2672be20775 to your computer and use it in GitHub Desktop.
Fix for heightForHeaderInSection as it is called before viewForHeaderInSection and the view size until I create it.
- (CGSize)tableView:(UITableView *)tableView sizeForHeaderLabelInSection:(NSInteger)section
{
NSString *text = [self tableView:tableView titleForHeaderInSection:section];
CGSize constraint = CGSizeMake(self.view.frame.size.width - kSectionTitleLeftMargin - kSectionTitleRightMargin, kMaxSectionTitleHeight);
return [text sizeWithFont:[self fontForSectionHeader] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return [self tableView:tableView sizeForHeaderLabelInSection:section].height + kSectionTitleTopMargin + kSectionTitleBottomMargin;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CGSize size = [self tableView:tableView sizeForHeaderLabelInSection:section];
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width + kSectionTitleLeftMargin + kSectionTitleRightMargin, size.height + kSectionTitleTopMargin + kSectionTitleBottomMargin)];
//headerView.contentMode = UIViewContentModeScaleToFill;
// Add the label
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSectionTitleLeftMargin,
kSectionTitleTopMargin,
size.width,
size.height)];
// put stuff to set up my headerLabel here...
[headerView addSubview:headerLabel];
// Return the headerView
return headerView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment