UITableView rectForHeaderInSection
- (CGRect)rectForHeaderInSection:(NSInteger)section
Parameters
section
An index number identifying a section of the table view. Plain-style table views always have a section index of zero.
Return Value of [UITableView rectForHeaderInSection]
A rectangle defining the area in which the table view draws the section header.
UITableView rectForHeaderInSection example.
- (IBAction) scrollToToday:(BOOL)animate {
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:animate];
if (animate == NO) [self showFirstHeaderLine:NO];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[self showFirstHeaderLine:YES];
}
- (void) showFirstHeaderLine:(BOOL)animate {
CGRect headerRect = [self.tableView rectForHeaderInSection:1];
CGPoint scrollPoint = headerRect.origin;
scrollPoint.y -= headerRect.size.height;
[self.tableView setContentOffset:scrollPoint animated:animate];
}
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:animate];
if (animate == NO) [self showFirstHeaderLine:NO];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[self showFirstHeaderLine:YES];
}
- (void) showFirstHeaderLine:(BOOL)animate {
CGRect headerRect = [self.tableView rectForHeaderInSection:1];
CGPoint scrollPoint = headerRect.origin;
scrollPoint.y -= headerRect.size.height;
[self.tableView setContentOffset:scrollPoint animated:animate];
}
Example of [UITableView rectForHeaderInSection].
CGRect rowRect = [table rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
CGRect headerRect = [table rectForHeaderInSection:0];
rowRect.origin.y -= headerRect.size.height;
rowRect.size.height += headerRect.size.height;
[table scrollRectToVisible:rowRect animated:YES]; // UITableView is a subclass of UIScrollView
CGRect headerRect = [table rectForHeaderInSection:0];
rowRect.origin.y -= headerRect.size.height;
rowRect.size.height += headerRect.size.height;
[table scrollRectToVisible:rowRect animated:YES]; // UITableView is a subclass of UIScrollView
UITableView rectForHeaderInSection example.
#define CGSizesMaxWidth(sz1, sz2) MAX((sz1).width, (sz2).width)
#define CGSizesAddHeights(sz1, sz2) (sz1).height + (sz2).height
+ (CGSize)sizeForTableView:(UITableView *)tableView {
CGSize tableViewSize = CGSizeMake(0, 0);
NSInteger numberOfSections = [tableView numberOfSections];
for (NSInteger section = 0; section < numberOfSections; section++) {
// Factor in the size of the section header
CGRect rect = [tableView rectForHeaderInSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
// Factor in the size of the section
rect = [tableView rectForSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
// Factor in the size of the footer
rect = [tableView rectForFooterInSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
}
return tableViewSize;
}
#define CGSizesAddHeights(sz1, sz2) (sz1).height + (sz2).height
+ (CGSize)sizeForTableView:(UITableView *)tableView {
CGSize tableViewSize = CGSizeMake(0, 0);
NSInteger numberOfSections = [tableView numberOfSections];
for (NSInteger section = 0; section < numberOfSections; section++) {
// Factor in the size of the section header
CGRect rect = [tableView rectForHeaderInSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
// Factor in the size of the section
rect = [tableView rectForSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
// Factor in the size of the footer
rect = [tableView rectForFooterInSection:section];
tableViewSize = CGSizeMake(CGSizesMaxWidth(tableViewSize, rect.size), CGSizesAddHeights(tableViewSize, rect.size));
}
return tableViewSize;
}