Skip to content

Instantly share code, notes, and snippets.

@yusuke024
Last active August 29, 2015 14:00
Show Gist options
  • Save yusuke024/2769da7b9570b07ae8a8 to your computer and use it in GitHub Desktop.
Save yusuke024/2769da7b9570b07ae8a8 to your computer and use it in GitHub Desktop.
Prevent table section header views from sticking at the top of the table view
// Implement this in the UITableView's subclass
// Please note that you have to return an instance of UITableViewHeaderFooterView's subclass in tableView:viewForHeaderInSection:
//
- (void)layoutSubviews {
[super layoutSubviews];
NSArray *visibleRows = [self indexPathsForVisibleRows];
NSMutableIndexSet *sections = [[NSMutableIndexSet alloc] init];
for (NSIndexPath *indexPath in visibleRows) {
[sections addIndex:indexPath.section];
}
[sections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
UIView *headerView = [self headerViewForSection:idx];
CGRect headerFrame = headerView.frame;
NSIndexPath *firstRow = [NSIndexPath indexPathForRow:0 inSection:idx];
CGRect firstRowFrame = [self rectForRowAtIndexPath:firstRow];
headerFrame.origin.y = firstRowFrame.origin.y - headerFrame.size.height;
headerView.frame = headerFrame;
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment