Saturday, June 8, 2013

UITableView tableView heightForHeaderInSection example in Objective C (iOS).


UITableView tableView heightForHeaderInSection

Asks the delegate for the height to use for the header of a particular section.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

Parameters of [UITableView tableView heightForHeaderInSection]
tableView
The table-view object requesting this information.
section
An index number identifying a section of tableView .

Return Value
A floating-point value that specifies the height (in points) of the header for section.

Discussion of [UITableView tableView heightForHeaderInSection]
This method allows the delegate to specify section headers with varying heights.

Special Considerations
Prior to iOS 5.0, table views would automatically resize the heights of headers to 0 for sections where tableView:viewForHeaderInSection: returned a nil view. In iOS 5.0 and later, you must return the actual height for each section header in this method.

UITableView tableView heightForHeaderInSection example.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 1 )
        return 0.000001f;
    else return 44.0f; // put 22 in case of plain one..
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001f; //removing section footers
}

Example of [UITableView tableView heightForHeaderInSection].
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return kFirstSectionHeaderHeight;
    return [self sectionHeaderHeight];
}

UITableView tableView heightForHeaderInSection example.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{   
    return [self tableView:tableView sizeForHeaderLabelInSection:section].height + kSectionTitleTopMargin + kSectionTitleBottomMargin;
}

End of UITableView tableView heightForHeaderInSection example article.