Last active
August 29, 2015 14:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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