UITableViewDataSource tableView titleForHeaderInSection
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Parameters of [UITableViewDataSource tableView titleForHeaderInSection]
tableView
The table-view object asking for the title.
section
An index number identifying a section of tableView .
Return Value
A string to use as the title of the section header. If you return nil , the section will have no title.
Discussion of [UITableViewDataSource tableView titleForHeaderInSection]
The table view uses a fixed font style for section header titles. If you want a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView:viewForHeaderInSection: instead.
UITableViewDataSource tableView titleForHeaderInSection example.
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//TEMP SECTION STRING HOLDER:
NSString *sectionHeader = nil;
//SET TITLE FOR EACH SECTION:
if(section == 0) {
sectionHeader = @"Section Header No. 1";
}
if(section == 1) {
sectionHeader = @"Section Header No. 2";
}
if(section == 2) {
sectionHeader = @"Section Header No. 3";
}
//RETURN THE SECTION HEADER FOR EACH SECTION:
return sectionHeader;
}
//TEMP SECTION STRING HOLDER:
NSString *sectionHeader = nil;
//SET TITLE FOR EACH SECTION:
if(section == 0) {
sectionHeader = @"Section Header No. 1";
}
if(section == 1) {
sectionHeader = @"Section Header No. 2";
}
if(section == 2) {
sectionHeader = @"Section Header No. 3";
}
//RETURN THE SECTION HEADER FOR EACH SECTION:
return sectionHeader;
}
Example of [UITableViewDataSource tableView titleForHeaderInSection].
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
/* If datasource methdod is implemented, get´s the name of sections*/
SEL nameForSection = @selector(nameForSection:);
if ( (self.dataSource) && [self.dataSource respondsToSelector:nameForSection] )
{
NSString *titleSection = [self.dataSource nameForSection:section];
return titleSection;
}
return nil;
{
/* If datasource methdod is implemented, get´s the name of sections*/
SEL nameForSection = @selector(nameForSection:);
if ( (self.dataSource) && [self.dataSource respondsToSelector:nameForSection] )
{
NSString *titleSection = [self.dataSource nameForSection:section];
return titleSection;
}
return nil;
UITableViewDataSource tableView titleForHeaderInSection example.
Store the titles of sections in an array as,
NSArray *sectionTitles = [NSArray arrayWithObjects:@"Frozen", @"Fruit", @"Salads", @"Vegetables", nil];
And modify your titleForHeaderInSection method as,
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *result = [sectionTitles objectAtIndex:section];
//....
return result;
}
Modify didSelectRowAtIndexPath as,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//...
NSLog(@"Header title: %@", [sectionTitles objectAtIndex:indexPath.section]);
//...
}
Another option is to use the below method in didSelectRowAtIndexPath
NSLog(@"Header title: %@", [self tableView:tableView titleForHeaderInSection:indexPath.section]);
NSArray *sectionTitles = [NSArray arrayWithObjects:@"Frozen", @"Fruit", @"Salads", @"Vegetables", nil];
And modify your titleForHeaderInSection method as,
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *result = [sectionTitles objectAtIndex:section];
//....
return result;
}
Modify didSelectRowAtIndexPath as,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//...
NSLog(@"Header title: %@", [sectionTitles objectAtIndex:indexPath.section]);
//...
}
Another option is to use the below method in didSelectRowAtIndexPath
NSLog(@"Header title: %@", [self tableView:tableView titleForHeaderInSection:indexPath.section]);